In my MongoDB collection, each documents has a field score
and a filed query
, so I am trying to group documents by query
, i.e., get groups of documents with the same query
filed, and meanwhile, I only want those groups containing files with score
= 3 (range of score
is 0,1,2,3). Here is my first aggregation clause:
agg = [{
"$group": {
"_id": "$query",
"src_ids": {"$push": "$src_id"},
"sources": {"$push": "$source"},
"scores": {"$push": "$score"}
}
}]
So I guess, I have to add a $match
regarding the maximum in scores
in agg
, but how should I do that?
===========================
Edit
if we have 6 files:
{"query": "bread", "score": 2, ...}
{"query": "bread", "score": 1, ...}
{"query": "meat", "score": 2, ...}
{"query": "meat", "score": 3, ...}
{"query": "fruit", "score": 0, ...}
{"query": "fruit", "score": 3, ...}
So here by the using the above aggregation clause: db.collection.aggregate(agg)
, we have 3 groups of: bread
, meat
, fruit
. However, if we consider the score issue, bread should not be returned.