Multi-key Indexing on an entire array with MongoDB?

MongoDBDatabaseBig Data Analytics

Let us first create a collection with documents −

> db.demo277.insertOne({"details":[{"FirstName":"John"}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e48fb21dd099650a5401a52")
}
> db.demo277.insertOne({"details":[{"FirstName":"David"}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e48fb27dd099650a5401a53")
}
> db.demo277.insertOne({"details":[{"FirstName":"Chris"}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e48fb2bdd099650a5401a54")
}

Display all documents from a collection with the help of find() method −

> db.demo277.find();

This will produce the following output −

{ "_id" : ObjectId("5e48fb21dd099650a5401a52"), "details" : [ { "FirstName" : "John" } ] }
{ "_id" : ObjectId("5e48fb27dd099650a5401a53"), "details" : [ { "FirstName" : "David" } ] }
{ "_id" : ObjectId("5e48fb2bdd099650a5401a54"), "details" : [ { "FirstName" : "Chris" } ] }

Following is the query to implement multi-key indexing on an entire array. It does scan full documents −

> db.demo277.find({"details":{FirstName:"David"}});

This will produce the following output −

{ "_id" : ObjectId("5e48fb27dd099650a5401a53"), "details" : [ { "FirstName" : "David" } ] }
raja
Published on 31-Mar-2020 13:15:36
Advertisements