仅显示 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" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP