要选择特定的列,您可以忽略其余的列,即隐藏这些列,将它们设置为0。让我们首先创建一个包含文档的集合-
> db.demo415.insertOne({"ClientName":"Robert","ClientCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e72329db912067e57771adc") } > db.demo415.insertOne({"ClientName":"David","ClientCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e7232acb912067e57771add") } > db.demo415.insertOne({"ClientName":"Bob","ClientCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e7232b4b912067e57771ade") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo415.find();
这将产生以下输出-
{ "_id" : ObjectId("5e72329db912067e57771adc"), "ClientName" : "Robert", "ClientCountryName" : "US" } { "_id" : ObjectId("5e7232acb912067e57771add"), "ClientName" : "David", "ClientCountryName" : "UK" } { "_id" : ObjectId("5e7232b4b912067e57771ade"), "ClientName" : "Bob", "ClientCountryName" : "AUS" }
以下是选择特定列的查询。在这里,我们忽略了其余的列以显示“ ClientCountryName”列-
> db.demo415.find({},{_id:0,ClientName:0});
这将产生以下输出-
{ "ClientCountryName" : "US" } { "ClientCountryName" : "UK" } { "ClientCountryName" : "AUS" }