jeudi 5 février 2015

MongoDB Sparse Index Query


I have around 40 million records in my collections but some million documents doesn't contain a field "catId".


Sample Document :


{ "_id": ObjectId("5458b1a24669c5c0d66ae658"), "host": "zombie.com", "aid": "22706048", "cat": "Celebs", "catIds": [[""], ["-2128958273", "1898055", "-2128682902"], ["-2128958273", "1898055", "-2128682902", ""]]"count": NumberLong(1), "date": NumberLong(1394562600) }


I need to fetch the records on host and date basis for a specific catid field.


{ "$match" : { "host" : "zombie.com" , "date" : { "$gt" : 1417372200 , "$lte" : 1420050600}}} { "$unwind" : "$catIds"} {"$match" : { "catIds" : "-2128958273"} } { "$project" : { "count" : 1 , "_id" : 0 , "host" : 1 , "cat" : 1 , "articleId" : 1}} { "$group" : { "_id" : { "aid" : "$aid"} , "count" : { "$sum" : "$count"} , "cat" : { "$first" : "$cat"}}}


I have created two indexes :


Index 1 : "host_1_date_-1" Index 2 : "catIds" : 1 , spare :true


My question is : whenever i make a match query and after the catId match query, how it will response. Does it will match only those records which are in sparse indexed OR It will fetch all matched docs and not use any catid index.





Aucun commentaire:

Enregistrer un commentaire