首先让我们创建一个包含文档的集合-
> 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" }