3

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")
        ]

3 Answers 3

3

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")
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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
            }
        },

    ]



);

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.