find()
与点表示法一起使用以执行递归搜索。首先让我们创建一个包含文档的集合-
> db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":101,"ClientName":"Chris"},{"ClientId":102,"ClientName":"Robert"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a118b50a6c6dd317ad99") } > db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":110,"ClientName":"David"},{"ClientId":112,"ClientName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a12fb50a6c6dd317ad9a") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.findOperationDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cd9a118b50a6c6dd317ad99"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" }, { "ClientId" : 102, "ClientName" : "Robert" } ] } { "_id" : ObjectId("5cd9a12fb50a6c6dd317ad9a"), "ClientDetails" : [ { "ClientId" : 110, "ClientName" : "David" }, { "ClientId" : 112, "ClientName" : "Mike" } ] }
以下是find()
使用点表示法实现递归搜索的查询-
> db.findOperationDemo.find({"ClientDetails.ClientId":110});
这将产生以下输出-
{ "_id" : ObjectId("5cd9a12fb50a6c6dd317ad9a"), "ClientDetails" : [ { "ClientId" : 110, "ClientName" : "David" }, { "ClientId" : 112, "ClientName" : "Mike" } ] }