要仅查询字段名称,请将fieldName设置为0,即要隐藏的fieldName。让我们创建一个包含文档的集合-
> db.demo650.insertOne({_id:101,details:{Name:"Chris",Age:21}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo650.insertOne({_id:102,details:{Name:"Bob",Age:22}}); { "acknowledged" : true, "insertedId" : 102 } > db.demo650.insertOne({_id:103,details:{Name:"Sam",Age:20}}); { "acknowledged" : true, "insertedId" : 103 } > db.demo650.insertOne({_id:104,details:{Name:"Robert",Age:24}}); { "acknowledged" : true, "insertedId" : 104 }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo650.find();
这将产生以下输出-
{ "_id" : 101, "details" : { "Name" : "Chris", "Age" : 21 } } { "_id" : 102, "details" : { "Name" : "Bob", "Age" : 22 } } { "_id" : 103, "details" : { "Name" : "Sam", "Age" : 20 } } { "_id" : 104, "details" : { "Name" : "Robert", "Age" : 24 } }
以下是如何仅查询MongoDB中的字段名称。我们想隐藏详细信息字段,因此我们将其设置为0-
> db.demo650.find({},{details:0});
这将产生以下输出-
{ "_id" : 101 } { "_id" : 102 } { "_id" : 103 } { "_id" : 104 }