

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How do I query a MongoDB collection?
To query or return a MongoDB collection, use getCollection(). Let us create a collection with documents −
> db.demo294.insertOne({"EmployeeId":101,"EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a1a5d93261e4bc9ea36") } > db.demo294.insertOne({"EmployeeId":102,"EmployeeName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a255d93261e4bc9ea37") } > db.demo294.insertOne({"EmployeeId":103,"EmployeeName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a335d93261e4bc9ea38") }
Display all documents from a collection with the help of find() method −
> db.demo294.find();
This will produce the following output −
{ "_id" : ObjectId("5e4d4a1a5d93261e4bc9ea36"), "EmployeeId" : 101, "EmployeeName" : "Chris" } { "_id" : ObjectId("5e4d4a255d93261e4bc9ea37"), "EmployeeId" : 102, "EmployeeName" : "Bob" } { "_id" : ObjectId("5e4d4a335d93261e4bc9ea38"), "EmployeeId" : 103, "EmployeeName" : "David" }
Following is the query to get MongoDB collection −
> db.getCollection('demo294').find({ ... "EmployeeId":{$in:[101,103]}});
This will produce the following output −
{ "_id" : ObjectId("5e4d4a1a5d93261e4bc9ea36"), "EmployeeId" : 101, "EmployeeName" : "Chris" } { "_id" : ObjectId("5e4d4a335d93261e4bc9ea38"), "EmployeeId" : 103, "EmployeeName" : "David" }
- Related Questions & Answers
- How do I display the indexes of a collection in MongoDB?
- MongoDB query to rename a collection?
- How can I rename a collection in MongoDB?
- Query MongoDB collection starting with _?
- MongoDB: How to query a collection named “version”?
- MongoDB query to pull array element from a collection?
- MongoDB query to remove entire data from a collection
- MongoDB query to update a specific document from a collection
- How do I sort natural in MongoDB?
- MongoDB query to find a specific city record from a collection
- MongoDB query to find last object in collection?
- MongoDB query to remove entire array from collection?
- How do I split a numerical query result in MySQL?
- How do I change a MongoDB user's password?
- MongoDB query to add a document in an already created collection
Advertisements