How to get objects inside an array if it meets a given condition using mongodb 3.0.0

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.

+1
source share
2 answers

request clarification:

  • $ match - match documents for criteria - limit documents for processing
  • $ relind - econstructs od an array field from input documents to output a document for each element. Each output document is an input document with an array field value replaced by an element.
  • $ match - matching documents for criteria
  • $ group is the opposite of $ unwind in our case - so we recreate an array of results

If you expect such a document:

 { "_id" : 2, "od" : [{ "oid" : 1234, "ca" : ISODate("2016-05-11T13:20:10.718Z") }, { "oid" : 2345, "ca" : ISODate("2016-05-12T13:20:10.718Z") } ] }, { "_id" : 1, "od" : [{ "oid" : 2345, "ca" : ISODate("2016-05-11T13:20:10.718Z") }, { "oid" : 57766, "ca" : ISODate("2016-05-13T13:20:10.718Z") } ] } 

You can use the query below:

 db.userDetail.aggregate([{ $match : { "od.ca" : { $lt : new Date(new Date().setDate(new Date().getDate() + 2)), $gte : new Date(new Date().setDate(new Date().getDate() - 4)) } } }, { $unwind : "$od" }, { $match : { "od.ca" : { $lt : new Date(new Date().setDate(new Date().getDate() + 2)), $gte : new Date(new Date().setDate(new Date().getDate() - 4)) } } }, { $group : { _id : "$_id", od : { $push : "$od" } } } ]) 
+1
source

The following query gave me the desired result. If you use mongodb-2.6.X before 3.0.X, you can use this solution.

 var object = {st : "10/05/2016", et : "13/05/2016"}; db.userDetail.aggregate( [ { $match: { "od.ca": { '$gte': new Date(object.st), '$lte': new Date(object.et) }, "lo" : {$ne : 'd'} } }, { $project: { em: 1, fna : 1, lna : 1, ca :1, od: { "$setDifference": [{ "$map": { "input": "$od", "as": "o", "in": { "$cond":[ { "$and": [ { "$gte": [ "$$o.ca", new Date(object.st) ] }, { "$lte": [ "$$o.ca", new Date(object.et) ] }, { "$ne": [ "$$o.oid", ObjectID(config.pid.toString()) ] } ] }, "$$o",false] } } },[false] ] } } }, {$sort : {_id : 1}} ]; ) 

If you are using 3.2.X, use $ filter to get the result.

0
source

All Articles