【问题标题】:Cannot sort arrays using unwind or reduce mongodb aggregation pipline无法使用展开或减少 mongodb 聚合管道对数组进行排序
【发布时间】:2019-01-04 09:47:35
【问题描述】:

我在 mongodb 聚合管道中对数组进行排序时遇到问题。我在 stackoverflow 中找不到答案,所以我发布了我的问题

我在 mongodb 中有一些具有这种结构的数据

{ 
      testOne:{ scores:[1, 5, 8]},
      testTwo:{scores:[3, 4, 5]},
      testThree:{scores:[9,0,1]},
      ........
      testFifty:{scores:[0,8,1]}
}

基本上是一系列的许多测试和这些测试的分数(这是一个说明问题的简化示例)。

头撞墙几个小时后,我发现我只能展开两个数组,再展开,也没有返回任何数据。

所以我无法展开所有数组和分组以从数据库中获取每个测试分数的数组

db.collection.aggregate([
    {
        {$unwind: '$testOne.scores'},
        {$unwind: '$testTwo.scores'},
        {$unwind: '$testThree.scores'},
        {$group:{
            _id: {},
           concatTestOne: { $push: "$testOne.scores"},
           concatTestTwo: { $push: "$testTwo.scores"},
           concatTestThree: { $push: "$testThree.scores"}
    }
}

令我沮丧的是,即使我只是展开,结果数组也没有正确排序,请参见下文:

db.collection.aggregate([
    {
        {$unwind: '$testOne.scores'},
        {$group:{
            _id: {},
           concatTestOne: { $push: "$testOne.scores"}
        }
    {$sort:{concatTestOne: 1}},
}

结果是 [1,5,8,3,4,5,....,9,0,1]。没有排序

因此,为了获得所有测试分数,我使用 reduce 来展平分组阶段产生的嵌套数组,没有“展开”,例如:

db.collection.aggregate([
    {
        {$group:{
            _id: {},
            concatTestOne: { $push: "$testOne.scores"}
        },
    {$addFields:{

        testOneScores:  {$reduce: {
        input: "$concatTestOne",
        initialValue: [ ],
        in: { $concatArrays: [ "$$value", "$$this" ] }
        }
    }
}

结果数组再次没有排序。谁能告诉我我做错了什么。数组很大(长度约为 3500)只是 mongoDB 聚合不处理大型数组吗?

非常感谢任何 cmets 我花了很多时间尝试对我的数组进行排序并注意到到目前为止的工作

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    据我了解,这是您需要的查询,否则请更新您的示例输出。

    db.col.aggregate([
        {$unwind: '$testOne.scores'},
        {$unwind: '$testTwo.scores'},
        {$unwind: '$testThree.scores'},
        {"$sort" : {"testOne.scores" : -1,"testTwo.scores" : -1,"testThree.scores" : -1}},
        {$group:{
            _id: {},
           concatTestOne: { $addToSet: "$testOne.scores"},
           concatTestTwo: { $addToSet: "$testTwo.scores"},
           concatTestThree: { $addToSet: "$testThree.scores"}
            }
        }
    ])
    

    【讨论】:

    • 感谢您的回答。但是当我尝试展开两个以上的数组时,返回的数据为空。我认为展开两个数组是 mongoDB 可以做的限制。无论如何,当我刚刚进行测试并在一个 array 上使用 unwind 时,排序没有效果。如果你能回答一些事情,我不知道你为什么把 $sort 放在双引号中,是正确的语法(当我这样做时我的代码错误);第二,我是否正确,你可以展开多少数组是有限制的,第三,这段代码对你是否有效。
    • 是的,这对我来说很好用。我得到这个输出 /* 1 */ { "_id" : {}, "concatTestOne" : [ 1.0, 5.0, 8.0 ], "concatTestTwo" : [ 3.0, 4.0, 5.0 ], "concatTestThree" : [ 0.0, 1.0, 9.0 ], "concatTestFifty" : [ 0.0, 1.0, 8.0 ] }
    • 如果你使用更多,放松没有限制,它需要一些时间。
    • 我正在使用 mongo shell,您可以使用任何带双引号或不带双引号的东西,无论如何两者都可以正常工作。这不是问题。
    • 我的查询是 query db.col.aggregate([ {"$unwind": '$testOne.scores'},{"$unwind": '$testTwo.scores'},{" $unwind": '$testThree.scores'},{"$unwind": '$testFifty.scores'}, {"$sort" : {"testOne.scores" : -1,"testTwo.scores" : -1 ,"testThree.scores" : -1,"testFifty.scores" : -1}}, {"$group":{ "_id": {},"concatTestOne": { "$addToSet": "$testOne.scores "},"concatTestTwo": { "$addToSet": "$testTwo.scores"},"concatTestThree": { "$addToSet": "$testThree.scores"},"concatTestFifty": { "$addToSet": " $testFifty.scores"} } } ])
    猜你喜欢
    • 2019-12-26
    • 1970-01-01
    • 2020-10-12
    • 2021-05-30
    • 2020-08-20
    • 2017-02-22
    • 1970-01-01
    • 2019-06-29
    • 2023-01-17
    相关资源
    最近更新 更多