如何在 MongoDB 查询中选定特定列?
要选定特定列,您可以忽略其余列,即,要隐藏这些列,请将它们设置为 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" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP