mongodb Mongo 按数组长度排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9040161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:26:51  来源:igfitidea点击:

Mongo order by length of array

mongodbsizesql-order-by

提问by Evgenius

Lets say I have mongo documents like this:

假设我有这样的 mongo 文件:

Question 1

问题 1

{
    answers:[
       {content: 'answer1'},
       {content: '2nd answer'}
    ]
}

Question 2

问题2

{
    answers:[
       {content: 'answer1'},
       {content: '2nd answer'}
       {content: 'The third answer'}
    ]
}

Is there a way to order the collection by size of answers?

有没有办法按答案的大小对集合进行排序?

After a little research I saw suggestions of adding another field, that would contain number of answers and use it as a reference but may be there is native way to do it?

经过一些研究,我看到了添加另一个领域的建议,该领域将包含许多答案并将其用作参考,但可能有本地方法可以做到这一点?

回答by Eve Freeman

I thought you might be able to use $size, but that's only to find arrays of a certain size, not ordering.

我以为您可以使用$size,但这只是为了找到特定大小的数组,而不是排序。

From the mongo documentation: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size

来自 mongo 文档:http: //www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size

You cannot use $size to find a range of sizes (for example: arrays with more than 1 element). If you need to query for a range, create an extra size field that you increment when you add elements. Indexes cannot be used for the $size portion of a query, although if other query expressions are included indexes may be used to search for matches on that portion of the query expression.

您不能使用 $size 来查找大小范围(例如:具有超过 1 个元素的数组)。如果您需要查询范围,请创建一个额外的大小字段,在添加元素时增加该字段。索引不能用于查询的 $size 部分,但如果包含其他查询表达式,则索引可用于搜索查询表达式该部分的匹配项。

Looks like you can probably fairly easily do this with the new aggregation framework, edit:which isn't out yet. http://www.mongodb.org/display/DOCS/Aggregation+Framework

看起来您可以使用新的聚合框架很容易地做到这一点,编辑:尚未发布。 http://www.mongodb.org/display/DOCS/Aggregation+Framework

UpdateNow the Aggregation Framework is out...

现在更新聚合框架已经出来......

> db.test.aggregate([
  {$unwind: "$answers"}, 
  {$group: {_id:"$_id", answers: {$push:"$answers"}, size: {$sum:1}}}, 
  {$sort:{size:1}}]);
{
"result" : [
    {
        "_id" : ObjectId("5053b4547d820880c3469365"),
        "answers" : [
            {
                "content" : "answer1"
            },
            {
                "content" : "2nd answer"
            }
        ],
        "size" : 2
    },
    {
        "_id" : ObjectId("5053b46d7d820880c3469366"),
        "answers" : [
            {
                "content" : "answer1"
            },
            {
                "content" : "2nd answer"
            },
            {
                "content" : "The third answer"
            }
        ],
        "size" : 3
    }
  ],
  "ok" : 1
}

回答by Oleg

I use $projectfor this:

$project为此使用:

db.test.aggregate([
    {
        $project : { answers_count: {$size: { "$ifNull": [ "$answers", [] ] } } }
    }, 
    {   
        $sort: {"answers_count":1} 
    }
])

It also allows to include documents with empty answers. But also has a disadvantage (or sometimes advantage): you should manually add all needed fields in $projectstep.

它还允许包含答案为空的文档。但也有一个缺点(或有时是优点):您应该$project逐步手动添加所有需要的字段。

回答by bhavin jalodara

you can use mongodb aggregation stage $addFieldswhich will add extra field to store count and then followed by $sortstage.

您可以使用 mongodb 聚合阶段$addFields,它将添加额外的字段来存储计数,然后是$sort阶段。

db.test.aggregate([
    {
        $addFields: { answers_count: {$size: { "$ifNull": [ "$answers", [] ] } } }
    }, 
    {   
        $sort: {"answers_count":1} 
    }
])