-
Type:
Question
-
Resolution: Done
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
I have a multi-key index (key1: 1, key2: 1, timestamp: -1) in an event-based database (i.e. thousands of documents per key1 and key2, ordered by timestamp).
What I want: Perform a query to return only the latest events, grouped by key1 and key2 - without scanning all past events!
However, when performing a query like the following, MongoDB actually scans all past events:
collection.aggregate([
{
"$match": {
"key1": "someKey1",
}
},
{
"$sort": {
"timestamp": -1
}
},
{
"$group": {
"_id": {
"key1": "$key1",
"key2": "$key2",
},
'timestamp': { $first: '$timestamp' },
}
}
]
When I run 'explain' on the query, it says
"indexBounds" : { "key1" : [ "[\"someKey1\"]" ], "key2" : [ "[MinKey, MaxKey]" ], "timestamp" : [ "[MaxKey, MinKey]" // <-- All past events are scanned. ] },
Is it possible to avoid this behavior, and make MongoDB only scan the latest object?