0

I need to find a user with X _id and I need the most current weightTracker.date usually it will be the last one.

I've tried db.user.find({_id:userid}, {weightTracker:1}).sort({"weightTracker.date":-1}) With little success on ordering my results. But most important that always returns all the results and I am only interested in the most current one.

How Could I get the most recent object in weightTracker given the object's date in user X?

User

_id
weightTracker[{ manyproperties:menyproperties, date:date}]

enter image description here

4

1 回答 1

0

您可能想尝试聚合方法(有关详细示例,您可以查看http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/

db.user.aggregate( { $match: { "_id": ObjectId("52426846311fc102641ef332") } } , 
                   { $unwind: "$weightTracker"},  
                   {$group: { _id: null, maxDate: {$max: "$weightTracker" } } }  
                 )
于 2013-09-25T04:41:48.820 回答