I want to project all the objects from an array if it matches the given condition.
I have the following data
{ _id : 1, em : ' abc@12s.net ', name : 'NewName', od : [ { "oid" : ObjectId("1234"), "ca" : ISODate("2016-05-05T13:20:10.718Z") }, { "oid" : ObjectId("2345"), "ca" : ISODate("2016-05-11T13:20:10.718Z") }, { "oid" : ObjectId("57766"), "ca" : ISODate("2016-05-13T13:20:10.718Z") } ] }, { _id : 2, em : ' ab6c@xyz.net ', name : 'NewName2', od : [ { "oid" : ObjectId("1234"), "ca" : ISODate("2016-05-11T13:20:10.718Z") }, { "oid" : ObjectId("2345"), "ca" : ISODate("2016-05-12T13:20:10.718Z") }, { "oid" : ObjectId("57766"), "ca" : ISODate("2016-05-05T13:20:10.718Z") } ] }
I want to get all the objects from the od array if "od.ca" is between the range, say if it can be more than the 10th and less than the 15th of May.
I tried using the aggregated mongodb method and I am new to this method. My request is below.
db.userDetail.aggregate( { $match: { 'od.ca': { '$gte': '10/05/2016', '$lte': '15/05/2016' }, lo: { '$ne': 'd' } } }, { $redact: { $cond: { if: { $gte: [ "$$od.ca", '10/05/2016' ], $lte : ["$$od.ca" , '15/05/2016'] }, then: "$$DESCEND", else: "$$PRUNE" } } })
When I try to use this command, we get an error: -
assert: command failed: {"errmsg": "exception: use variable undefined: od", "code": 17276, "OK": 0}: aggregate failed
Since I am using mongodb 3.0.0, I cannot use $ fiter . So I tried using $ redact .
Can someone tell me I'm wrong? Is it set correctly?
Question is also mentioned. Since I do not use 3.2 mongodb (as I already mentioned), I can not use the accepted answer to the question.