要查找字段等于给定整数的文档,请使用find()
。让我们创建一个包含文档的集合-
> db.demo472.insertOne({"Project_Id":-101,"ProjectName":"Online Customer Tracking"});{ "acknowledged" : true, "insertedId" : ObjectId("5e80586cb0f3fa88e227907a") } > db.demo472.insertOne({"Project_Id":101,"ProjectName":"Online Banking System"});{ "acknowledged" : true, "insertedId" : ObjectId("5e805884b0f3fa88e227907b") } > db.demo472.insertOne({"Project_Id":102,"ProjectName":"Online Library System"});{ "acknowledged" : true, "insertedId" : ObjectId("5e805893b0f3fa88e227907c") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo472.find();
这将产生以下输出-
{ "_id" : ObjectId("5e80586cb0f3fa88e227907a"), "Project_Id" : -101, "ProjectName" : "Online Customer Tracking" } { "_id" : ObjectId("5e805884b0f3fa88e227907b"), "Project_Id" : 101, "ProjectName" : "Online Banking System" } { "_id" : ObjectId("5e805893b0f3fa88e227907c"), "Project_Id" : 102, "ProjectName" : "Online Library System" }
以下是在MongoDB集合中查找文档的查询,其中字段等于给定的整数值-
> db.getCollection('demo472').find({"Project_Id":-101});
这将产生以下输出-
{ "_id" : ObjectId("5e80586cb0f3fa88e227907a"), "Project_Id" : -101, "ProjectName" : "Online Customer Tracking" }