Vishnudas
Vishnudas

Reputation: 229

Fetch distinct values from Mongo DB nested array and output to a single array

given below is my data in mongo db.I want to fetch all the unique ids from the field articles ,which is nested under the jnlc_subjects index .The result should contain only the articles array with distinct object Ids.

Mongo Data

{
"_id" : ObjectId("5c9216f1a21a4a31e0c7fa56"),
"jnlc_journal_category" : "Biology",
"jnlc_subjects" : [ 
    {
        "subject" : "Conservation Biology",
        "views" : "123",
        "articles" : [ 
            ObjectId("5c4e93d0135edb6812200d5f"), 
            ObjectId("5c4e9365135edb6a12200d60"), 
            ObjectId("5c4e93a8135edb6912200d61")
        ]
    }, 
    {
        "subject" : "Micro Biology",
        "views" : "20",
        "articles" : [ 
            ObjectId("5c4e9365135edb6a12200d60"), 
            ObjectId("5c4e93d0135edb6812200d5f"), 
            ObjectId("5c76323fbaaccf5e0bae7600"), 
            ObjectId("5ca33ce19d677bf780fc4995")
        ]
    }, 
    {
        "subject" : "Marine Biology",
        "views" : "8",
        "articles" : [ 
            ObjectId("5c4e93d0135edb6812200d5f")
        ]
    }
]
}

Required result
I want to get output in following format

articles : [ 
            ObjectId("5c4e9365135edb6a12200d60"), 
            ObjectId("5c4e93a8135edb6912200d61"),
            ObjectId("5c76323fbaaccf5e0bae7600"), 
            ObjectId("5ca33ce19d677bf780fc4995"),
            ObjectId("5c4e93d0135edb6812200d5f")
        ]

Upvotes: 3

Views: 1694

Answers (3)

Rubin Porwal
Rubin Porwal

Reputation: 3845

According to description as mentioned into above question,as a solution to it please try executing following aggregate operation.

db.collection.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $match: {
                "_id": ObjectId("5c9216f1a21a4a31e0c7fa56")
            }
        },

        // Stage 2
        {
            $unwind: {
                path: "$jnlc_subjects",
            }
        },

        // Stage 3
        {
            $unwind: {
                path: "$jnlc_subjects.articles"

            }
        },

        // Stage 4
        {
            $group: {
                _id: null,
                articles: {
                    $addToSet: '$jnlc_subjects.articles'
                }
            }
        },

        // Stage 5
        {
            $project: {
                articles: 1,
                _id: 0
            }
        },

    ]



);

Upvotes: 1

Ashwanth Madhav
Ashwanth Madhav

Reputation: 1124

Try with this

db.collection.aggregate([
{
$unwind:{
 path:"$jnlc_subjects",
 preserveNullAndEmptyArrays:true
 }   
},
{
$unwind:{
 path:"$jnlc_subjects.articles",
 preserveNullAndEmptyArrays:true
 }   
},
{
$group:{
    _id:"$_id",
    articles:{
        $addToSet:"$jnlc_subjects.articles"
        }
    }
}
])

If you don't want to $group with _id ypu can use null instead of $_id

Upvotes: 1

Jitendra
Jitendra

Reputation: 3185

Try as below:

db.collection.aggregate([
        {
            $unwind: "$jnlc_subjects"
        },
        {
            $unwind: "$jnlc_subjects.articles"
        },
        { $group: {_id: null, uniqueValues: { $addToSet: "$jnlc_subjects.articles"}} }
    ])

Result:

{
    "_id" : null,
    "uniqueValues" : [
        ObjectId("5ca33ce19d677bf780fc4995"),
        ObjectId("5c4e9365135edb6a12200d60"),
        ObjectId("5c4e93a8135edb6912200d61"),
        ObjectId("5c4e93d0135edb6812200d5f"),
        ObjectId("5c76323fbaaccf5e0bae7600")
    ]
}

Upvotes: 3

Related Questions