Find mongoDB documents where an array field exists and not empty

This tutorial guides you to find mongoDB collection documents where an array field exists and and it is not empty. Also shows an example on how to find documents where an array field exists and it is empty.

Mongodb documents where array field exists and not empty

To find mongo records where an array field exists and not empty, you need to use element query operator $exists to check if array field exists and comparison query operator $ne that matches all values which are not equal to specified value (empty array).

For our example, you can create a sample collection called posts with data using this json. Then try the following commands and check the response as shown below.

The following example queries all documents where the tags is an array field which exists and it is not equal to empty.

> db.posts.find({tags:{$exists:true,$ne:[]}}).pretty();

Response:
---------

{
        "_id" : ObjectId("5063114bd386d8fadbd6b004"),
        "title" : "MongoDB OverView",
        "description" : "Overview and Introduction about MongoDB",
        "by_user" : "mongodb",
        "url" : "www.docs.mongodb.com",
        "tags" : [
                "#mongodb",
                "#mongo_tutor",
                "#tutorials"
        ],
        "likes" : 20,
        "comments" : [
                {
                        "user" : "Mishra",
                        "message" : "Nice tutorial",
                        "dateCreated" : ISODate("2011-02-25T02:15:00Z"),
                        "like" : 10
                },
                {
                        "user" : "Sonam",
                        "message" : "Not completely correct. Partially right.",
                        "dateCreated" : ISODate("2011-02-25T02:15:00Z"),
                        "like" : 12
                }
        ]
}
{
        "_id" : ObjectId("5063114bd386d8fadbd6b005"),
        "title" : "MongoDB OverView 2",
        "description" : "2 Overview and Introduction about MongoDB",
        "by_user" : "mongodb",
        "url" : "www.docs.mongodb.com",
        "tags" : [
                "#mongodb",
                "#mongo_tutor",
                "#tutorials"
        ],
        "likes" : 30,
        "comments" : [
                {
                        "user" : "Mishra",
                        "message" : "Nice tutorial",
                        "dateCreated" : ISODate("2011-02-25T02:15:00Z"),
                        "like" : 10
                },
                {
                        "user" : "Sonam",
                        "message" : "Not completely correct. Partially right.",
                        "dateCreated" : ISODate("2011-02-25T02:15:00Z"),
                        "like" : 12
                }
        ]
}
{
        "_id" : ObjectId("5063114bd386d8fadbd6b008"),
        "title" : "MongoDB Advantages",
        "description" : "Advantages of MongoDB",
        "by_user" : "sneppets",
        "url" : "https://docs.mongodb.com",
        "tags" : [
                "#mongodb",
                "#mongo_tutor",
                "#tutorials",
                "#advantages"
        ],
        "likes" : 50,
        "comments" : [
                {
                        "user" : "John",
                        "message" : "Nice tutorial",
                        "dateCreated" : ISODate("2019-02-25T02:15:00Z"),
                        "like" : 10
                },
                {
                        "user" : "Paul",
                        "message" : "Good one",
                        "dateCreated" : ISODate("2019-02-25T02:15:00Z"),
                        "like" : 12
                }
        ]
}

Mongodb documents where array field exists and empty

The following example queries all records where the tags is an array field which exists and it is equal to empty.

> db.posts.find({tags:{$exists:true,$eq:[]}}).pretty();

Response:
---------

{
        "_id" : ObjectId("5e6f4ecb2b916c3ea064c9f1"),
        "title" : "MongoDB Disadvantages",
        "description" : "Disadvantages of MongoDB",
        "by_user" : "admin",
        "url" : "http://docs.mongodb.com",
        "tags" : [ ],
        "likes" : 20,
        "comments" : [ ]
}

Also Read:

References:

Leave a Reply

avatar
  Subscribe  
Notify of