简体   繁体   中英

Find Object Inside nested Array mongodb nodejs

Suppose I need to get a specific document inside 'shows'(array) which is in an object in Array 'screens', How do I get this particular show using MongoDB Aggregation in my NodeJS project???

This my theaterCollection.

    "_id" : ObjectId("5fdd148e3a850f2980fc79b6"), 
    "theaterUser" : ObjectId("5fd237ad0279894d60045fd3"), 
**>>>>** "screens" : [
        {
            "ScreenName" : "Ad 1", 
            "Seats" : " 64", 
            "_id" : ObjectId("5fdd148e3a850f2980fc79b5"), 
  **>>>>** "shows" : [
                {
                 "MovieName" : "Guppy",                           *
                  "Screen" : "Ad 1",                              *
                 "Date" : ISODate("2020-12-12T00:00:00.000+0000"),* 
                 "time" : "13:30",                                *Get this One
                 "VIP" : "400",                                   *
                 "Premium" : "320",                               *
                 "Executive" : "170"                              * 
                }
            ]
        }, 
     
}```

You could do this querying with elemMatch or you could use mongodb aggregate for custom querying In Case of $elemMatch you could try this:

screens:{$elemMatch:{shows:$elemMatch:{MovieName:'Guppy'}}}

You could try "elemMatch" keyword for nested array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM