We have inserted the following report in the collection named customers as proven below −
db.users.insert(
{
"address": {
"city": "Los Angeles",
"state": "California",
"pincode": "123"
},
"tags": [
"music",
"cricket",
"blogs"
],
"name": "Tom Benzamin"
}
)
The above file incorporates an deal with sub-report and a tags array.
Indexing Array Fields
Suppose we need to go looking person files based totally at the person’s tags. For this, we will create an index on tags array within the collection.
Creating an index on array in turn creates separate index entries for each of its fields. So in our case whilst we create an index on tags array, separate indexes might be created for its values music, cricket and blogs.
To create an index on tags array, use the subsequent code −
>db.users.createIndex({"tags":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
>
After growing the index, we will seek on the tags area of the collection like this −
> db.users.find({tags:"cricket"}).pretty()
{
"_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
"address" : {
"city" : "Los Angeles",
"state" : "California",
"pincode" : "123"
},
"tags" : [
"music",
"cricket",
"blogs"
],
"name" : "Tom Benzamin"
}
>
To verify that proper indexing is used, use the subsequent provide an explanation for command −
>db.users.find({tags:"cricket"}).explain()
This gives you the subsequent end result −
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mydb.users",
"indexFilterSet" : false,
"parsedQuery" : {
"tags" : {
"$eq" : "cricket"
}
},
"queryHash" : "9D3B61A7",
"planCacheKey" : "04C9997B",
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"tags" : 1
},
"indexName" : "tags_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"tags" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"tags" : [
"[\"cricket\", \"cricket\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "Krishna",
"port" : 27017,
"version" : "4.2.1",
"gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
},
"ok" : 1
}
>
The above command resulted in "cursor" : "BtreeCursor tags_1" which confirms that right indexing is used.
Indexing Sub-Document Fields
Suppose that we want to search documents based on metropolis, state and pincode fields. Since these kinds of fields are a part of cope with sub-report discipline, we are able to create an index on all of the fields of the sub-file.
For developing an index on all of the 3 fields of the sub-report, use the following code −
>db.users.createIndex({"address.city":1,"address.state":1,"address.pincode":1})
{
"numIndexesBefore" : 4,
"numIndexesAfter" : 4,
"note" : "all indexes already exist",
"ok" : 1
}
>
Once the index is created, we will look for any of the sub-document fields utilizing this index as follows −
> db.users.find({"address.city":"Los Angeles"}).pretty()
{
"_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
"address" : {
"city" : "Los Angeles",
"state" : "California",
"pincode" : "123"
},
"tags" : [
"music",
"cricket",
"blogs"
],
"name" : "Tom Benzamin"
}
Remember that the query expression has to follow the order of the index unique. So the index created above might aid the subsequent queries −
>db.users.find({"address.city":"Los Angeles","address.state":"California"}).pretty()
{
"_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
"address" : {
"city" : "Los Angeles",
"state" : "California",
"pincode" : "123"
},
"tags" : [
"music",
"cricket",
"blogs"
],
"name" : "Tom Benzamin"
}
>