Dataset
> use temp_index_issue
> db.createCollection("mycollection")
> for (let i=0; i<900000; i++) { db.mycollection.insertOne({"name": "Place - "+ (i+1), "location": {"country": "India"}})};
> for (let i=900000; i<1000000; i++) { db.mycollection.insertOne({"name": "Place - "+ (i+1), "location": {"country": "USA"}})};
> for (let i=1000000; i<1050000; i++) { db.mycollection.insertOne({"name": "Place - "+ (i+1), "location": {"country": null}})};
> for (let i=1050000; i<1100000; i++) { db.mycollection.insertOne({"name": "Place - "+ (i+1)})};
I would like to count number of documents where location.country is "India" or field does not exists or field has null value.
> db.mycollection.countDocuments({"location.country": {"$in": ["India", null]}})
1000000 (0.493 sec)
Now if I set index on "location.country" field, it's taking much more time.
> db.mycollection.createIndex({"location.country": 1})
> db.mycollection.countDocuments({"location.country": {"$in": ["India", null]}})
1000000 (1.42 sec)
What I'm missing here?
count
operation how it uses index.