MongoDB 排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8206778/
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:19:33  来源:igfitidea点击:

MongoDB sorting

mongodb

提问by teggy

I want implement a "bump" feature for topics. Once a topic is bumped, it will have a new "bump_date" field. I want to sort it so that when there is a "bump_date" field, it will be sorted as if it was the "created" field. Here's an example of my db.topics:

我想为主题实现“碰撞”功能。一旦主题被碰撞,它将有一个新的“bump_date”字段。我想对它进行排序,以便当有一个“bump_date”字段时,它会被排序,就好像它是“创建”字段一样。这是我的 db.topics 示例:

{
    "text" : "test 1",
    "created" : "Sun Nov 20 2011 02:03:28 GMT-0800 (PST)"
},
{
    "text" : "test 2",
    "created" : "Sun Nov 18 2011 02:03:28 GMT-0800 (PST)"
},
{
    "text" : "test 3",
    "created" : "Sun Nov 17 2011 02:03:28 GMT-0800 (PST)",
    "bump_date: : "Sun Nov 19 2011 02:03:28 GMT-0800 (PST)"
}

I want the sort to return in the order of "test 1", "test 3", "test 2"

我希望排序以“测试 1”、“测试 3”、“测试 2”的顺序返回

回答by Brian Hicks

Sorting in MongoDB is done like so:

MongoDB 中的排序是这样完成的:

db.collection.find({ ... spec ... }).sort({ key: 1 })

db.collection.find({ ... spec ... }).sort({ key: 1 })

where 1is ascending and -1is descending.

哪里1是上升和-1下降。

In your specific example: db.topics.find().sort({ bump_date: 1 }), although it might be better to call it something like "updated_at".

在您的具体示例中:db.topics.find().sort({ bump_date: 1 }),尽管将其称为“updated_at”之类的内容可能会更好。

You'll also definitely want to put an index on your "bump_date" field.

您肯定还想在“bump_date”字段上放置索引。

回答by Tyler Brock

As Brian Hicks suggested, creating an additional updated_at field is the way to go. This way, when a document is created you can have created_at and updated_at initially be the same.

正如 Brian Hicks 所建议的,创建一个额外的 updated_at 字段是可行的方法。这样,当创建文档时,您可以使 created_at 和 updated_at 最初相同。

{
     "created_at": xxx,
     "updated_at": xxx
}

If you then "bump" the updated_at field by setting it to the current time when there is a a bump event you can sort on the updated_at field to achieve the ordering you desire.

如果您然后通过将其设置为当前时间来“碰撞”updated_at 字段,则您可以在发生碰撞事件时对 updated_at 字段进行排序以实现您想要的排序。

回答by Oneide Luiz Schneider

Also:

还:

db.collection.find( { $query: {}, $orderby: { column : -1 } } )

where 1 is ascending and -1 is descending.

其中 1 为升序,-1 为降序。

回答by DhruvPathak

Currently it is not possible in mongodb to do a sort based on user defined criteria over multiple columns.eg. here the function would have been to return bump_dateif it is set,else return created

目前在 mongodb 中不可能根据用户定义的标准对多个列进行排序。例如。这里的函数bump_date如果设置了就返回,否则返回created

Either you will have to use a server-side or client-side code as mentioned here :

您将不得不使用此处提到的服务器端或客户端代码:

Mongo complex sorting?

Mongo复杂排序?

or if you want to stay with basic quering and sorting, you shall :

或者,如果您想继续使用基本的查询和排序,您应该:

  • create a key bump_dateequivalent to createdwhenever a new record is created. This will notbe a data overhead, as you can expect every topic of yours to be bumped once in a while in future,hence bump_datefield will eventually be added. So add it from the start itself.

  • Whenever the article is bumped,update the field bump_date.

  • 创建一个bump_date相当于created每当创建新记录时的键。这不会是数据开销,因为您可以预期您的每个主题将来bump_date都会偶尔被碰撞,因此最终会添加字段。所以从一开始就添加它。

  • 每当文章被碰撞时,更新该字段bump_date

Your example documents will look like this with this change :

通过此更改,您的示例文档将如下所示:

{
    "text" : "test 1",
    "created" : "Sun Nov 20 2011 02:03:28 GMT-0800 (PST)",
    "bump_date" : "Sun Nov 20 2011 02:03:28 GMT-0800 (PST)"
},
{
    "text" : "test 2",
    "created" : "Sun Nov 18 2011 02:03:28 GMT-0800 (PST)",
    "bump_date" : "Sun Nov 18 2011 02:03:28 GMT-0800 (PST)" 
},
{
    "text" : "test 3",
    "created" : "Sun Nov 17 2011 02:03:28 GMT-0800 (PST)",
    "bump_date: : "Sun Nov 19 2011 02:03:28 GMT-0800 (PST)"
}

You shall ensureIndexon bump_date field. Now you can query the required data easily.

你应该ensureIndex在bump_date 字段上。现在您可以轻松查询所需数据。

db.topics.find().sort({ bump_date: 1 })