
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 Articles
- How do I display the indexes of a collection in MongoDB?
- MongoDB: How to query a collection named “version”?
- MongoDB query to rename a collection?
- Query MongoDB collection starting with _?
- How can I rename a collection in MongoDB?
- MongoDB query to remove entire data from a collection
- MongoDB query to pull array element from a collection?
- MongoDB query to update a specific document from a collection
- MongoDB query to find last object in collection?
- MongoDB query to remove entire array from collection?
- MongoDB query to find a specific city record from a collection
- MongoDB query to add a document in an already created collection
- MongoDB query for capped sub-collection in an array
- MongoDB collection query to exclude some fields in find()?
- How do I sort natural in MongoDB?

Advertisements