
- 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
MongoDB Query to select records having a given key?
To select records having a given key, you can use $exists operator. The syntax is as follows −
db.yourCollectionName.find( { yourFieldName: { $exists : true } } ).pretty();
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"John","StudentAge":21,"StudentMathMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5c8b7be780f10143d8431e0f") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Carol","StudentMathMarks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5c8b7bfc80f10143d8431e10") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam","StudentAge":26,"StudentMathMarks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5c8b7c1280f10143d8431e11") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam","StudentMathMarks":98}); { "acknowledged" : true, "insertedId" : ObjectId("5c8b7c2180f10143d8431e12") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.selectRecordsHavingKeyDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8b7be780f10143d8431e0f"), "StudentName" : "John", "StudentAge" : 21, "StudentMathMarks" : 78 } { "_id" : ObjectId("5c8b7bfc80f10143d8431e10"), "StudentName" : "Carol", "StudentMathMarks" : 89 } { "_id" : ObjectId("5c8b7c1280f10143d8431e11"), "StudentName" : "Sam", "StudentAge" : 26, "StudentMathMarks" : 89 } { "_id" : ObjectId("5c8b7c2180f10143d8431e12"), "StudentName" : "Sam", "StudentMathMarks" : 98 }
Here is the query to select records having a given key −
> db.selectRecordsHavingKeyDemo.find( { StudentMathMarks : { $exists : true } } ).pretty();
The following is the output −
{ "_id" : ObjectId("5c8b7be780f10143d8431e0f"), "StudentName" : "John", "StudentAge" : 21, "StudentMathMarks" : 78 } { "_id" : ObjectId("5c8b7bfc80f10143d8431e10"), "StudentName" : "Carol", "StudentMathMarks" : 89 } { "_id" : ObjectId("5c8b7c1280f10143d8431e11"), "StudentName" : "Sam", "StudentAge" : 26, "StudentMathMarks" : 89 } { "_id" : ObjectId("5c8b7c2180f10143d8431e12"), "StudentName" : "Sam", "StudentMathMarks" : 98 }
- Related Articles
- How to query a key having space in its name with MongoDB?
- MySQL query to select top 10 records?
- MySQL query to select bottom n records
- MySQL query to select records with a particular date?
- MongoDB query to get date records in a range
- MySQL query to select records beginning from a specific id
- MongoDB query to add matched key to list after query?
- How to write a MySQL query to select first 10 records?
- MySQL query to select records with a particular date and time?
- MongoDB query to select distinct and count?
- MongoDB query to limit subdocuments having the given fields of the 'projection'
- MongoDB query to fetch date records (ISODate format) in a range
- MongoDB Query to search for records only in a specific hour?
- MySQL query to select records approaching in next 12 hours?
- MongoDB query to insert but limit the total records

Advertisements