要重试字段的子集,请在中使用点符号find()
。让我们创建一个包含文档的集合-
> db.demo307.insertOne({ ... "CleintId":101, ... "ClientDetails":{"ClientFirstName":"Chris","Age":34}, ... "ClientCountryName":"US" ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4eab88f8647eb59e56203c") } > db.demo307.insertOne({ ... "CleintId":102, ... "ClientDetails":{"ClientFirstName":"David","Age":31}, ... "ClientCountryName":"UK" ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4eab97f8647eb59e56203d") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo307.find();
这将产生以下输出-
{ "_id" : ObjectId("5e4eab88f8647eb59e56203c"), "CleintId" : 101, "ClientDetails" : { "ClientFirstName" : "Chris", "Age" : 34 }, "ClientCountryName" : "US" } { "_id" : ObjectId("5e4eab97f8647eb59e56203d"), "CleintId" : 102, "ClientDetails" : { "ClientFirstName" : "David", "Age" : 31 }, "ClientCountryName" : "UK" }
以下是从MongoDB中检索字段子集的查询-
> db.demo307.find( {"ClientDetails.ClientFirstName":'David'}, {"ClientDetails.Age" : 0,"ClientCountryName":0,CleintId:0,_id:0});
这将产生以下输出-
{ "ClientDetails" : { "ClientFirstName" : "David" } }