
- 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 to check if a field in MongoDB is [] or {}?
To check if a field in MongoDB is [] or {}, you can use the following syntax −
db.yourCollectionName.find({ "yourOuterFieldName": { "$gt": {} }, "yourOuterFieldName.0": { "$exists": false } });
Let us first create a collection with documents -
> db.checkFieldDemo.insert([ ... { _id: 1010, StudentDetails: {} }, ... { _id: 1011, StudentDetails: [ { StudentId: 1 } ] }, ... { _id: 1012, StudentDetails: [ {} ] }, ... { _id: 1013 }, ... { _id: 1014, StudentDetails: null}, ... { _id: 1015, StudentDetails: { StudentId: 1 } } ... ]); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 6, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
Following is the query to display all documents from a collection with the help of find() method −
> db.checkFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : 1010, "StudentDetails" : { } } { "_id" : 1011, "StudentDetails" : [ { "StudentId" : 1 } ] } { "_id" : 1012, "StudentDetails" : [ { } ] } { "_id" : 1013 } { "_id" : 1014, "StudentDetails" : null } { "_id" : 1015, "StudentDetails" : { "StudentId" : 1 } }
Following is the query to check if a field in MongoDB is [] or {} −
> db.checkFieldDemo.find({ ... "StudentDetails": { "$gt": {} }, ... "StudentDetails.0": { "$exists": false } ... });
This will produce the following output −
{ "_id" : 1015, "StudentDetails" : { "StudentId" : 1 } }
- Related Articles
- How to check if field is a number in MongoDB?
- How to check if field is null or empty in MySQL?
- How to check if a text field is empty or not in swift?
- Check if a field contains a string in MongoDB?
- How can I check whether a field exists or not in MongoDB?
- Check whether field exist in MongoDB or not?
- How to check empty field in a MongoDB collection?
- Check if value exists for a field in a MongoDB document?
- How to apply a condition only if field exists in MongoDB?
- How to search for string or number in a field with MongoDB?
- How to increment a field in MongoDB?
- How do I check whether a field contains null value in MongoDB?
- Check whether a field is empty or null in MySQL?
- How to query for records where field is null or not set in MongoDB?
- Check that Field Exists with MongoDB?

Advertisements