
- 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
Check if a list is not empty in MongoDB?
For this, use the $size operator. Let us first create a collection with documents −
> db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":["John","David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd99e8bf3115999ed511f7") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":["Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd99e9bf3115999ed511f8") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd99ebbf3115999ed511f9") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[null]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd99f2bf3115999ed511fa") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd99f6bf3115999ed511fb") }
Following is the query to display all documents from a collection with the help of find() method −
> db.checkIfListIsNotEmptyDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cdd99e8bf3115999ed511f7"), "UserFriendGroup" : [ "John", "David" ] } { "_id" : ObjectId("5cdd99e9bf3115999ed511f8"), "UserFriendGroup" : [ "Carol" ] } { "_id" : ObjectId("5cdd99ebbf3115999ed511f9"), "UserFriendGroup" : [ ] } { "_id" : ObjectId("5cdd99f2bf3115999ed511fa"), "UserFriendGroup" : [ null ] } { "_id" : ObjectId("5cdd99f6bf3115999ed511fb"), "UserFriendGroup" : [ ] }
Following is the query to check if a list is not empty −
> db.checkIfListIsNotEmptyDemo.find({'UserFriendGroup': {'$not': {'$size': 0}}});
This will produce the following output −
{ "_id" : ObjectId("5cdd99e8bf3115999ed511f7"), "UserFriendGroup" : [ "John", "David" ] } { "_id" : ObjectId("5cdd99e9bf3115999ed511f8"), "UserFriendGroup" : [ "Carol" ] } { "_id" : ObjectId("5cdd99f2bf3115999ed511fa"), "UserFriendGroup" : [ null ] }
- Related Articles
- Check if a directory is not empty in Java
- Check if a String is not empty ("") and not null in Java
- How to check if a list is empty in Python?
- How to check if a C# list is empty?
- C# program to check whether a list is empty or not
- Python program to check whether a list is empty or not?
- Check if a table is empty or not in MySQL using EXISTS
- What is best way to check if a list is empty in Python?
- Java Program to check if a string is empty or not
- How do you check if a ResultSet is empty or not in JDBC?
- How to check if a text field is empty or not in swift?
- MySQL query to check if database is empty or not?
- How to check cursor array list is empty or not in Android sqlite?
- How to Check if an Array is Empty or Not in Java
- Python program to check if the string is empty or not

Advertisements