从 MongoDB 中检索字段子集
在 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" } }
广告