Anuj
Anuj

Reputation: 78

How to query an array in an array of objects in mongodb?

I am pretty new to mongodb, and I am not able to query in my mongo collection.

Structure :

"chapterId":1,
"videos" : [ 
        {
            "videoId" : "1",
            "videoName" : "about",
            "duration" : "12:36",
            "tags":["business", "design"]
        }, 
        {
            "videoId" : "2",
            "videoName" : "course",
            "duration" : "04:00",
            "tags":["technology", "design"]
        }
]

I need to select all videos with the tag "business" in chapterId 1.

Can this be done without changing the structure of my collection ?

Upvotes: 0

Views: 55

Answers (1)

Neo-coder
Neo-coder

Reputation: 7840

You should use aggregation so below query will help you

db.collectionName.aggregate(
          {"$unwind":"$videos"},
          {"$match":{"chapterId":1,"videos.tags":"business"}}
          )

Upvotes: 3

Related Questions