在 MongoDB 中进行单列投影?
首先,让我们使用文档创建一个集合 -
> db.demo216.insertOne({"ClientName":"John","ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e351003d395bdc213470c") } > db.demo216.insertOne({"ClientName":"Bob","ClientAge":32}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e351703d395bdc213470d") } > db.demo216.insertOne({"ClientName":"Mike","ClientAge":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e351c03d395bdc213470e") }
使用 find() 方法从集合中显示所有文档 -
> db.demo216.find();
这将生成以下输出 -
{ "_id" : ObjectId("5e3e351003d395bdc213470c"), "ClientName" : "John", "ClientAge" : 34 } { "_id" : ObjectId("5e3e351703d395bdc213470d"), "ClientName" : "Bob", "ClientAge" : 32 } { "_id" : ObjectId("5e3e351c03d395bdc213470e"), "ClientName" : "Mike", "ClientAge" : 35 }
以下是仅投影单列的查询 -
> db.demo216.find({},{_id:0,"ClientAge":0});
这将生成以下输出 -
{ "ClientName" : "John" } { "ClientName" : "Bob" } { "ClientName" : "Mike" }
广告