mongoDB distinct & where in same query?

前端 未结 3 1116
無奈伤痛
無奈伤痛 2020-12-08 00:29

Let\'s say I have the following documents

Article { Comment: embedMany }

Comment { Reply: embedMany }

Reply { email: string, ip: string }

3条回答
  •  无人及你
    2020-12-08 00:45

    Maybe you could try this

    db.Article.aggregate([
    {$unwind: "$Comment"},
    {$unwind: "$Comment.Reply"},
    {$match: {"Comment.Reply.email": "xxx"}},
    {$group: {_id: "$Comment.Reply.ip"}}
    ])
    

    The result of example should be

    /* 1 */
    {
        "_id" : "192.168.1.1"
    }
    
    /* 2 */
    {
        "_id" : "128.168.1.1"
    }
    

提交回复
热议问题