Find documents in mongodb collection where the array field contains specific values 

This tutorial guides you to find the documents in mongodb collection where the array field contains specific values using query operations on array fields. Let’s see what mongo provides to perform query on array with example.

Query array field containing element with specific value

To query the documents in mongo collection where array field contains at least one element with the specified values, you need to use the filter { <field>: [<value1>, …] }  where <value1> is the element value.

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

The following example queries all documents where the tags is an array field that contains the string values  [“#mongodb”,”#mongo_tutor”,”#tutorials”] and another examples where the tags contains the string values [“#mongodb”,”#mongo_tutor”,”#tutorials”,”#advantages”].

Example 1

> db.posts.find({tags:["#mongodb","#mongo_tutor","#tutorials"]}).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
                }
        ]
}

Example 2

> db.posts.find({tags:["#mongodb","#mongo_tutor","#tutorials","#advantages"]}).pretty();

Response:
---------
{
        "_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
                }
        ]
}

Also See

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments