仅显示MongoDB集合中所有文档中的单个字段
投影意味着只有选定的字段必须可见。如果您想使其可见,请将字段设置为1。
首先让我们创建一个包含文档的集合-
> db.demo384.insertOne({"StudentName":"Chris Brown","StudentCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b67a022064be7ab44e7f2")
}
> db.demo384.insertOne({"StudentName":"David Miller","StudentCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b67ab22064be7ab44e7f3")
}
> db.demo384.insertOne({"StudentName":"John Doe","StudentCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b67b422064be7ab44e7f4")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo384.find();
这将产生以下输出-
{ "_id" : ObjectId("5e5b67a022064be7ab44e7f2"), "StudentName" : "Chris Brown", "StudentCountryName" : "US" }
{ "_id" : ObjectId("5e5b67ab22064be7ab44e7f3"), "StudentName" : "David Miller", "StudentCountryName" : "AUS" }
{ "_id" : ObjectId("5e5b67b422064be7ab44e7f4"), "StudentName" : "John Doe", "StudentCountryName" : "UK" }以下是仅显示单个字段并忽略其余字段的查询-
> db.demo384.find({},{_id:0,StudentName:0});这将产生以下输出-
{ "StudentCountryName" : "US" }
{ "StudentCountryName" : "AUS" }
{ "StudentCountryName" : "UK" }热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短