2

I have documents like this:

{
    "_id" : ObjectId("5b3ced158735f1196d73a743"),
    "cid" : 1,
    "foo" : [
        {
            "k" : "sport",
            "v" : "climbing"
        },
        {
            "k" : "sport",
            "v" : "soccer"
        },
        {
            "k" : "sport",
            "v" : "soccer"
        }
    ]
}

This Query just return the documents which contains a soccer field.

db.coll.find({foo:{$elemMatch:{ v: "soccer"}} }, {"foo.$" : 1,cid:1})

returns:

{ "_id" : ObjectId("5b3ced158735f1196d73a743"), "cid" : 1, "node" : [ { "k" : "sport", "v" : "climbing" } ] }

But I want to know, how many soccer-Elements are in each returned document. How can I count them?

2
  • Just wanted to know if I got your question. You expect output : 2 ? Am I right? Commented Jul 5, 2018 at 12:41
  • Yeah, cid and count. There are a lot of documents like this. And I need for example: {cid: 1,count:4}, {cid:3,count: 2} Commented Jul 5, 2018 at 12:48

2 Answers 2

2
db.coll.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $match: {
                foo: {
                    $elemMatch: {
                        v: 'soccer'
                    }
                }
            }
        },

        // Stage 2
        {
            $unwind: {
                path: '$foo'
            }
        },

        // Stage 3
        {
            $project: {
                cid: 1,
                count: {
                    $cond: {
                        if: {
                            $eq: ['$foo.v', 'soccer']
                        },
                        then: {
                            $sum: 1
                        },
                        else: 0
                    }
                }
            }
        },

        // Stage 4
        {
            $group: {
                _id: '$cid',
                total_count: {
                    $sum: '$count'
                }
            }
        }

    ]



);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use below query to $filter and $size the filtered array to count no of matching occurrences.

db.coll.aggregate([
  {"$project":{
    "cid":1,
    "count":{
      "$size":{
        "$filter":{
          "input":"$foo",
          "cond":{"$eq":["$$this.v","soccer"]
          }
        }
      }
    }
  }}
])

2 Comments

Cool, but how can I just return fields with count > 0?
You can add match stage after project. Something like {"$match":{"count":{"$gt":0}}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.