2

The following is my moviedb collection:

 "_id" : ObjectId("59b9501600fcb397d6acd5bb"),
  "theatreid" : 2,
 "name" : "carnival cinemas",
 "location" : "kanjulmarg",
 "address" : "sec 2,kanjul, Mumbai, Maharashtra 400703",
 "shows" : [
      {
              "mname" : "bareily ki barfi",
              "timings" : [
                      10,
                      13,
                      14,
                      16,
                      22
              ]
      },

      {
              "mname" : "Toilet:ek prem katha",
              "timings" : [
                      8,
                      9,
                      14,
                      16,
                      20,
                      23
              ]
      }
  ]


  "_id" : ObjectId("59b9506500fcb397d6acd5bc"),
   "theatreid" : 3,
  "name" : "pheonix pvr",
 "location" : "kurla",
 "address" : "sec 26,kurla, Mumbai, Maharashtra 400701",
 "shows" : [
      {
              "mname" : "shubh mangal savdhan",
              "timings" : [
                      9,
                      11,
                      15,
                      18,
                      20
              ]
      },
      {
              "mname" : "Toilet:ek prem katha",
              "timings" : [
                      8,
                      9,
                      14,
                      16,
                      20,
                      23
              ]
      }
 ]

I want to display the shows with movie timings (assume it to be 24hr format) greater than 14 (2 pm). I used the following query:

db.moviedb.find( {"shows.timings": {$gt:14}},  {shows: {$elemMatch: {timings:{$gt: 14}}}}).pretty()

It gives the following output:

Actual

   "_id" : ObjectId("59b94fc900fcb397d6acd5ba"),
   "shows" : [
           {
                   "mname" : "bareily ki barfi",
                   "timings" : [
                           10,
                           13,
                           15,
                           17,
                           21
                   ]
           }
   ]


   "_id" : ObjectId("59b9501600fcb397d6acd5bb"),
   "shows" : [
           {
                   "mname" : "bareily ki barfi",
                   "timings" : [
                           10,
                           13,
                           14,
                           16,
                           22
                   ]
           }
   ]

But as it is observed ,it displays those elements also along ,which are not satisfying the condition (like 10,13 ). i simply want the only those elements to be displayed which are satisfying the result. I want the output in the following way .

Expected

   "_id" : ObjectId("59b94fc900fcb397d6acd5ba"),
   "shows" : [
           {
                   "mname" : "bareily ki barfi",
                   "timings" : [

                           15,
                           17,
                           21
                   ]
           }
   ]


   "_id" : ObjectId("59b9501600fcb397d6acd5bb"),
   "shows" : [
           {
                   "mname" : "bareily ki barfi",
                   "timings" : [

                           16,
                           22
                   ]
           }
   ]

This is the output i am getting even after using $filter ,but it works well if i give the input array manually.

    db.moviedb.aggregate([    {       $project: {         " shows.timings": 
{             $filter: {                input: "$shows.timings",                
as: "item",
 cond: { $gte: [ "$$item", 22 ] }             }          }       }    } ])
"_id" : ObjectId("59b94fc900fcb397d6acd5ba"), " shows" : { "timings" : [ [ 
10, 13, 15, 17, 21 ], [ 8, 11, 14, 17, 20, 22 ] ] } }
"_id" : ObjectId("59b9501600fcb397d6acd5bb"), " shows" : { "timings" : [ [ 
10, 13, 14, 16, 22 ], [ 8, 9, 14, 16, 20, 23 ] ] } }
"_id" : ObjectId("59b9506500fcb397d6acd5bc"), " shows" : { "timings" : [ [ 
9, 11, 15, 18, 20 ], [ 8, 9, 14, 16, 20, 23 ] ] } }

What needs to be done? Please help.Thanks :)

radhika
  • 39
  • 4
  • It looks like you can use `$filter` to do what you want - https://docs.mongodb.com/manual/reference/operator/aggregation/filter/ – pizzarob Sep 13 '17 at 18:23
  • Possible duplicate of [Find in Double Nested Array MongoDB](https://stackoverflow.com/questions/29071748/find-in-double-nested-array-mongodb) – s7vr Sep 13 '17 at 18:28
  • @realseanp ,i used the $filter,still it displays the values which are not satisfying the condition .I simply want to display the elements which are satisfying the condition only in the shows.timings array – radhika Sep 14 '17 at 14:33
  • @realseanp please check the post above, i have added the output of $filter . – radhika Sep 14 '17 at 15:27
  • Have you tried the answer from the linked post ? – s7vr Sep 14 '17 at 15:42
  • @Veeram can you help me with the query in my case ,what it should be ? – radhika Sep 14 '17 at 16:06

0 Answers0