Finding documents in MongoDB collection where a field is equal to given integer value?

MongoDBDatabaseBig Data Analytics

To find documents where a field is equal to given integer, use find(). Let us create a collection with documents −

> 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")
}

Display all documents from a collection with the help of find() method −

> db.demo472.find();

This will produce the following output −

{ "_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" }

Following is the query to find documents in MongoDB collection where a field is equal to given integer value −

> db.getCollection('demo472').find({"Project_Id":-101});

This will produce the following output −

{ "_id" : ObjectId("5e80586cb0f3fa88e227907a"), "Project_Id" : -101, "ProjectName" : "Online
Customer Tracking" }
raja
Published on 11-May-2020 09:41:41
Advertisements