排序聚合 addToSet 结果 [英] Sorting aggregation addToSet result

查看:10
本文介绍了排序聚合 addToSet 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 $addToSet 的结果作为排序数组?

我尝试扩展管道并 $unwind 数组,对其进行排序并再次分组,但结果仍然没有排序.

数组非常大,我尽量避免在应用程序中对它们进行排序.

<上一页>文档示例:{"_id" : ObjectId("52a84825cc8391ab188b4567"),身份证":129624消息":样本",日期":2013 年 12 月 9 日,17:34:34",dt":ISODate(2013-12-09T17:34:34.000Z"),}

查询:

<上一页>db.uEvents.aggregate([{$match:{dt:{$gte:新日期(2014,01,01),$lt:新日期(2015,01,17)}}},{$sort : {dt : 1}},{$组:{_ID : {id : "$id", 年 : {'$year' : "$dt"}, 月 : {'$month' : "$dt"}, 日: {'$dayOfMonth': "$dt"}},dt : {$addToSet : "$dt"}}}]);

解决方案

是的,这是可能的,但方法不同.我只是为此提供我自己的数据,但你会明白这个概念.

我的样本:

{ "数组" : [ 2, 4, 3, 5, 2, 6, 8, 1, 2, 1, 3, 5, 9, 5 ] }

我将就此半引用"CTO 并声明 Sets 被认为是无序.

<块引用>

有一个实际的 JIRA,Google groups 声明是这样的.因此,让我们从Elliot"中理解并接受这是这样的.

所以如果你想要一个有序的结果,你必须用这样的阶段来按摩

db.collection.aggregate([//初始展开{"$unwind": "$array"},//做你的 $addToSet 部分{"$group": {"_id": null, "array": {"$addToSet": "$array" }}},//再次展开{"$unwind": "$array"},//按你想要的方式排序{"$sort": { "array": 1} },//对常规数组使用 $push{"$group": { "_id": null, "array": {"$push": "$array" }}}])

然后做任何事情.但是现在您的数组已排序.

Is there a way to get result of $addToSet as sorted array ?

I tried to expand the pipeline and $unwind the array, sort it and group it again , but still the result isn't sorted.

The arrays are pretty big and i try to avoid sort them in the the application.


Document Example :

    {
      "_id" : ObjectId("52a84825cc8391ab188b4567"),
      "id" : 129624
      "message" : "Sample",
      "date" : "12-09-2013,17:34:34",
      "dt" : ISODate("2013-12-09T17:34:34.000Z"),

    }

Query :


    db.uEvents.aggregate(
    [
      {$match : {dt : {$gte : new Date(2014,01,01) , $lt : new Date(2015,01,17)}}}
      ,{$sort : {dt : 1}}
      , {$group : {
        _id : {
                id : "$id"
                , year : {'$year' : "$dt"}
                , month : {'$month' : "$dt"}
                , day : {'$dayOfMonth' : "$dt"}
            }
        ,dt : {$addToSet : "$dt"}

      }}    
    ]

    );

解决方案

Yes it is possible, but approach it differently. I'm just provide my own data for this, but you'll get the concept.

My Sample:

{ "array" : [  2,  4,  3,  5,  2,  6,  8,  1,  2,  1,  3,  5,  9,  5 ] }

I'm going to "semi-quote" the CTO on this and state that Sets are considered to be unordered.

There is an actual JIRA, Google groups statement that goes something like that. So let's take it from "Elliot" and accept that this will be the case.

So if you want an ordered result, you have to massage that way with stages like this

db.collection.aggregate([

    // Initial unwind
    {"$unwind": "$array"},

    // Do your $addToSet part
    {"$group": {"_id": null, "array": {"$addToSet": "$array" }}},

    // Unwind it again
    {"$unwind": "$array"},

    // Sort how you want to
    {"$sort": { "array": 1} },

    // Use $push for a regular array
    {"$group": { "_id": null, "array": {"$push": "$array" }}}

])

And then do whatever. But now your array is sorted.

这篇关于排序聚合 addToSet 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆