Given a collection with documents such as:
{
"host" : "example.com",
"ips" : [
{
"ip" : NumberLong("1111111111"),
"timestamp" : NumberLong(1373970044)
},
{
"ip" : NumberLong("2222222222"),
"timestamp" : NumberLong(1234978746)
}
]
}
I need to return all documents with an ip value of X, but only if the associated timestamp for X is the highest timestamp in the ips array (so the above example document should not match a search for "2222222222" because that is not the IP with the most recent timestamp).
This is my first time doing anything much beyond fairly basic stuff in MongoDB so the closest I've been able to get is:
coll.aggregate({$match:{"ips.ip":X}},{$group:{"_id":"$host", "max":{$max:"$ips.timestamp"}}},{$sort:{"ips.timestamp":-1}}).result
Which obviously doesn't give me what I'm looking for, it returns anything with an ips.ip value of X. How do I return only documents where ip.ip is X only if X's associated timestamp is the highest for that ips array?