

- 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
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 Questions & Answers
- Check if a directory is not empty in Java
- How to check if a C# list is empty?
- How to check if a list is empty in Python?
- Java Program to check if a string is empty or not
- Check if a String is not empty ("") and not null in Java
- 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
- MySQL query to check if database is empty or not?
- What is best way to check if a list is empty in Python?
- 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?
- Check if a HashMap is empty in Java
- Check if list is sorted or not in Python
- Python program to check if the string is empty or not
Advertisements