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 :)