I have a collection with several documents. The documents might look like this:
{"brand" : "Mars",
"version" : 1}
{"brand" : "Mars",
"version" : 2}
{"brand" : "Bounty",
"version" : 4}
{"brand" : "Snickers",
"version" : 3}
I want to write a query to get just the brand one time with the most actual version. The query should be written in mongoengine or pymongo.
When I filter the collection for example with:
query_result = Sweets.objects.order_by("-version")
I get the following structure:
{"brand" : "Bounty",
"version" : 4}
{"brand" : "Snickers",
"version" : 3}
{"brand" : "Mars",
"version" : 2}
{"brand" : "Mars",
"version" : 1}
But I don't want two documents of "Mars". I just want the most recent one. Is there a way to write one query to get this result:
{"brand" : "Bounty",
"version" : 4}
{"brand" : "Snickers",
"version" : 3}
{"brand" : "Mars",
"version" : 2}