从Mongo DB嵌套数组中获取不同的值,然后输出到单个数组

时间:2019-04-10 13:18:46

标签: mongodb mongodb-query

下面给出的是我在mongo db中的数据。我想从嵌套在jnlc_subjects索引下的字段article中获取所有唯一ID。结果应仅包含具有不同对象ID的articles数组。

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

必填结果
我想以以下格式获取输出

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

3 个答案:

答案 0 :(得分:1)

请尝试以下操作:

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

结果:

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

答案 1 :(得分:1)

尝试一下

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

如果您不想与$group一起_id,则ypu可以使用null代替$_id

答案 2 :(得分:1)

根据上述问题的描述,作为解决方法,请尝试执行以下聚合操作。

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

    ]



);