To retrieve array values from a find query, use dot notation. Let us create a collection with documents −
> db.demo38.insertOne({"ClientDetails":[{"ClientId":101,"ClientName":"Chris"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176abccfb11e5c34d898d9") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":102,"ClientName":"David"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176ac7cfb11e5c34d898da") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":103,"ClientName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176ad0cfb11e5c34d898db") }
Display all documents from a collection with the help of find() method −
> db.demo38.find();
This will produce the following output −
{ "_id" : ObjectId("5e176abccfb11e5c34d898d9"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" } ] } { "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] } { "_id" : ObjectId("5e176ad0cfb11e5c34d898db"), "ClientDetails" : [ { "ClientId" : 103, "ClientName" : "Mike" } ] }
Following is the query to retrieve array values from a find query −
> db.demo38.find({"ClientDetails.ClientName":"David"});
This will produce the following output −
{ "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] }