c# - Unwind array and return the last element matching condition -
say have document structure looks like
[ { _id:1, logs : [ { time: 123, x: 24 }, { time: 212, x: 13 }, { time: 325, x: 34 } ] }, { _id:2, logs : [ { time: 63, x: 19 }, { time: 112, x: 25 }, { time: 205, x: 15 } ] } ] what i'm trying value of x time < n , _id = y
so example, if y=2, , n=115 want 25 return.
given time < 115 , greatest time matching filter in array?
i'm going in c#, convert shell syntax if can help?
thanks
edit:
so question i'm asking is... $elemmatch return document if finds 1 match, $elemmatch on time<115 , _id=2 return second document. if project array, 3 logs sub docs.
i'm therefore assuming need use aggregation filter projected array?
the following aggregation desired results:
db.collection.aggregate([ { "$match": {"_id": y, "logs.time": {"$lt": n} } }, { "$unwind": "$logs" }, { "$match": {"logs.time": {"$lt": n} } }, { "$group": { "_id": "$_id", "x": { "$max": "$logs.x" } } }, { "$project": { "_id": 0, "x": 1 } } ])
Comments
Post a Comment