MongoDB按联接结果进行排序并进行汇总查找

时间:2019-06-27 07:50:47

标签: mongodb

我有两个收藏集userpost

> db.user.find().pretty()
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9de"),
    "user_id" : NumberLong(1),
    "region" : "US",
    "is_join" : true
}
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9df"),
    "user_id" : NumberLong(2),
    "region" : "KR",
    "is_join" : true
}
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9e0"),
    "user_id" : NumberLong(3),
    "region" : "US",
    "is_join" : true
}
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9e1"),
    "user_id" : NumberLong(4),
    "region" : "US",
    "is_join" : true
}
{
    "_id" : ObjectId("5d1487fc1b48d9321ff5dc1f"),
    "user_id" : NumberLong(5),
    "region" : "US",
    "is_join" : true
}
> db.post.find().pretty()
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9e2"),
    "post_id" : NumberLong(1),
    "user_id" : NumberLong(3),
    "body" : "hi"
}
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9e3"),
    "post_id" : NumberLong(2),
    "user_id" : NumberLong(1),
    "body" : "hello"
}
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9e4"),
    "post_id" : NumberLong(3),
    "user_id" : NumberLong(2),
    "body" : "go"
}
{
    "_id" : ObjectId("5d1473bc1b48d9309580a9e5"),
    "post_id" : NumberLong(4),
    "user_id" : NumberLong(4),
    "body" : "python"
}
{
    "_id" : ObjectId("5d14941b1b48d93314907345"),
    "post_id" : NumberLong(5),
    "user_id" : NumberLong(1),
    "body" : "aa"
}

我想通过mongo聚合查找操作加入。

所以我这样查询。

db.user.aggregate([
    {
        '$match': {
            'region': 'US',
        }
    },
    {
        '$lookup': {
            'from': 'post',
            'localField': 'user_id',
            'foreignField': 'user_id',
            'as': 'user'
        }
    },
    {
        '$project': {
            '_id': 0,
            'user.post_id': 1
        }
    }
])

结果

{ "user" : [ { "post_id" : NumberLong(2) }, { "post_id" : NumberLong(5) } ] }
{ "user" : [ { "post_id" : NumberLong(1) } ] }
{ "user" : [ { "post_id" : NumberLong(4) } ] }
{ "user" : [ ] }

您知道post_id是无序的。

但是我想将其降序排列。

所需结果

{ "user" : [ { "post_id" : NumberLong(1) } ] }
{ "user" : [ { "post_id" : NumberLong(2) } ] }
{ "user" : [ { "post_id" : NumberLong(4) } ] }
{ "user" : [ { "post_id" : NumberLong(5) } ] }

更好

{ "user" : [ 1, 2, 4, 5 }] }

如何修改汇总查询?

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作以获取所需的内容:

1)$unwind用户数组

2)使用$sortpost_id对所有文档进行排序

3)将$group用作聚合查询的下一阶段,然后按user.post_id从所有文档中组成一个排序的user数组:

db.user.aggregate([
    {
        '$match': {
            'region': 'US',
        }
    },
    {
        '$lookup': {
            'from': 'post',
            'localField': 'user_id',
            'foreignField': 'user_id',
            'as': 'user'
        }
    },
    {
        $unwind : {
            path : "$user",
            preserveNullAndEmptyArrays : true
        }
    },
    {
        $sort : {
            'user.post_id' : 1
        }
    },
    {
        $group : {
            _id  : null,
            user : {
                $push : "$user.post_id"
            }
        }
    }
])

了解有关$sort$unwind$group的更多信息。

相关问题