0

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.

Bs He
  • 717
  • 1
  • 10
  • 22

2 Answers2

0

So the added match condition will look in the scores array for 3.

agg = [
{
    "$group": {
        "_id": "$query",
        "src_ids": {"$push": "$src_id"},
        "sources": {"$push": "$source"},
        "scores": {"$push": "$score"}
    }
},
{
    "$match": {
        "scores": 3
    }
}
]
Mani
  • 1,471
  • 1
  • 13
  • 19
0

Inspired by @Mani, in fact it's pretty simple but I am very new to Mongodb.

agg = [{
    "$group": {
        "_id": "$query",
        "src_ids": {"$push": "$src_id"},
        "sources": {"$push": "$source"},
        "scores": {"$push": "$score"},
        "max_score": {"$max": "$score"}
    }},
    {
    "$match": {
        "max_score": {"$eq": 3}
    }}
]
Bs He
  • 717
  • 1
  • 10
  • 22