要从查找查询中检索数组值,请使用点表示法。让我们创建一个包含文档的集合-
> 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") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo38.find();
这将产生以下输出-
{ "_id" : ObjectId("5e176abccfb11e5c34d898d9"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" } ] } { "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] } { "_id" : ObjectId("5e176ad0cfb11e5c34d898db"), "ClientDetails" : [ { "ClientId" : 103, "ClientName" : "Mike" } ] }
以下是从查找查询中检索数组值的查询-
> db.demo38.find({"ClientDetails.ClientName":"David"});
这将产生以下输出-
{ "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] }