当前位置: 动力学知识库 > 问答 > 编程问答 >

find all documents with common fields mongodb

问题描述:

I have a users collection:

{

id: [email protected],

uid: '57672373683'

}

{

id: [email protected],

uid: '25346346221'

}

{

id: [email protected],

uid: '57672373683'

}

I would like to run a query that will return all of the users with common uid field, for example here I will see usera and userc together because they have the same uid field.

Is it possible with a query in mongo ?

网友答案:

You can group users by using Aggregation Framework. The query will be like :

db.users.aggregate({$group : {_id : "$uid", users : {$addToSet : "$id"}}})

Results will look like :

"result" : [
    {
        "_id" : "25346346221",
        "users" : [
            "[email protected]"
        ]
    },
    {
        "_id" : "57672373683",
        "users" : [
            "[email protected]",
            "[email protected]"
        ]
    }
]
分享给朋友:
您可能感兴趣的文章:
随机阅读: