MongoDB查询单个字段
对于单个字段,请使用find()
。首先让我们创建一个包含文档的集合-
> db.demo10.insertOne({"StudentId":101,"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f68a7d7df943a7cec4f9b") } > db.demo10.insertOne({"StudentId":102,"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f68afd7df943a7cec4f9c") } > db.demo10.insertOne({"StudentId":103,"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f68b5d7df943a7cec4f9d") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.demo10.find();
这将产生以下输出-
{ "_id" : ObjectId("5e0f68a7d7df943a7cec4f9b"), "StudentId" : 101, "StudentName" : "Chris" } { "_id" : ObjectId("5e0f68afd7df943a7cec4f9c"), "StudentId" : 102, "StudentName" : "David" } { "_id" : ObjectId("5e0f68b5d7df943a7cec4f9d"), "StudentId" : 103, "StudentName" : "Bob" }
这是对单个字段的查询-
> db.demo10.find({},{"_id":0,"StudentId":0});
这将产生以下输出-
{ "StudentName" : "Chris" } { "StudentName" : "David" } { "StudentName" : "Bob" }