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}]
You might want to try aggregate method (for detailed examples, you can see 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" } } }
)