0

My documents look like this:

{
    "_id": "1",
    "tags": [ 
        { "code": "01-01", "type": "machine" },
        { "code": "04-06", "type": "gearbox" },
        { "code": "07-01", "type": "machine" } 
    ]
},
{
    "_id": "2",
    "tags": [
        { "code": "03-04","type": "gearbox" },
        { "code": "01-01", "type": "machine" },
        { "code": "04-11", "type": "machine" }
    ]
}

I want to get distinct codes only for tags whose type is "machine". so, for the example above, the result should be ["01-01", "07-01", "04-11"]. How do I do this?

1

3 Answers 3

1

Using $unwind and then $group with the tag as the key will give you each tag in a separate document in your result set:

db.collection_name.aggregate([
    {
        $unwind: "$tags"
    },
    {
        $match: { 
            "tags.type": "machine" 
        }
    },
    {
        $group: {
            _id: "$tags.code"
        }
    },
    {
        $project:{
            _id:false
            code: "$_id"
        }
    }
]);

Or, if you want them put into an array within a single document, you can use $push within a second $group stage:

db.collection_name.aggregate([
    {
        $unwind: "$tags"
    },
    {
        $match: { 
            "tags.type": "machine" 
        }
    },
    {
        $group: {
            _id: "$tags.code"
        }
    },
    {
        $group:{
            _id: null,
            codes: {$push: "$_id"}
        }
    }
]);

Another user suggested including an initial stage of { $match: { "tags.type": "machine" } }. This is a good idea if your data is likely to contain a significant number of documents that do not include "machine" tags. That way you will eliminate unnecessary processing of those documents. Your pipeline would look like this:

db.collection_name.aggregate([
    {
        $match: { 
            "tags.type": "machine" 
        }
    },
    {
        $unwind: "$tags"
    },
    {
        $match: { 
            "tags.type": "machine" 
        }
    },
    {
        $group: {
            _id: "$tags.code"
        }
    },
    {
        $group:{
            _id: null,
            codes: {$push: "$_id"}
        }
    }
]);
Sign up to request clarification or add additional context in comments.

2 Comments

I'd suggest to lead off with { $match: { "tags.type": "machine" } } to get rid of any documents that won't contribute.
Good suggestion. Updated the answer.
1
> db.foo.aggregate( [
... { $unwind : "$tags" },
... { $match : { "tags.type" : "machine" } },
... { $group : { "_id" : "$tags.code" } },
... { $group : { _id : null , "codes" : {$push : "$_id"} }}
... ] )
{ "_id" : null, "codes" : [ "04-11", "07-01", "01-01" ] }

Comments

1

A better way would be to group directly on tags.type and use addToSet on tags.code.

Here's how we can achieve the same output in 3 stages of aggregation :

db.name.aggregate([
  {$unwind:"$tags"},

  {$match:{"tags.type":"machine"}},

  {$group:{_id:"$tags.type","codes":{$addToSet:"$tags.code"}}} 
])

Output : { "_id" : "machine", "codes" : [ "04-11", "07-01", "01-01" ] }

Also, if you wish to filter out tag.type codes, we just need to replace "machine" in match stage with desired tag.type.

Comments

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.