
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Documents in MongoDB Where Field Equals Given Integer Value
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" }
Advertisements