4

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"                              * 
                }
            ]
        }, 
     
}```
Hasip Timurtas
  • 983
  • 2
  • 11
  • 20
anas_m__
  • 97
  • 6
  • Try something like this: `{ screens: { $elemMatch: { shows: $elemMatch: { MovieName: 'Guppy' }}}}` – Molda Dec 20 '20 at 18:59
  • Its not working, I am getting whole thing again..... – anas_m__ Dec 20 '20 at 19:18
  • Have a look at this https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – Molda Dec 21 '20 at 07:35

1 Answers1

1

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

Faiz P
  • 770
  • 1
  • 5
  • 17