$orderby 和 Sort 之间的 MongoDB 区别

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

MongoDB difference between $orderby and Sort

mongodbmongoose

提问by Sambhav Sharma

I want to fetch the latest document, which obviously is a single document, thus findOneshould work fine. But findOnehere returns the first document inserted. So I have two options now either use $orderBywith findOneor use .sort()function with .limit()in find()

我想获取最新的文档,这显然是一个文档,因此findOne应该可以正常工作。但findOne这里返回插入的第一个文档。所以我现在有两个选择,要么使用$orderBywithfindOne要么使用.sort()函数.limit()infind()

Using $orderBy it would look something like:

使用 $orderBy 它看起来像:

db.collection.findOne({$query:{},$orderby:{_id:-1}}) 

And using sort:

并使用排序:

db.collection.find().sort({_id:-1}).limit(1).pretty()

Both work fine, I just wanted to know which query should I prefer here? In terms of performance, or does both of them work the same way internally and there is no such difference between the two.

两者都工作正常,我只是想知道我应该在这里更喜欢哪个查询?在性能方面,还是两者内部工作方式相同,两者之间没有这种区别。

回答by Jeff Widman

As of Mongo 3.2, $orderbyis deprecated.

从 Mongo 3.2 开始,$orderby弃用

The docs explicitly say:

文件明确地说

The $orderby operator is deprecated. Use cursor.sort() instead.

$orderby 运算符已弃用。改用 cursor.sort() 。

Unfortunately, findOne()doesn't support the sort()method, so you'll need to switch to find():

不幸的是,findOne()不支持该sort()方法,因此您需要切换到find()

db.collection.find({}).sort({'key': -1}).limit(1)

This will return a cursor, so you'll then need to pull the first result from the cursor.

这将返回 a cursor,因此您需要从光标中提取第一个结果。

回答by Sammaye

They are the same and in fact the documentation page for $orderbyactually talks mostly about the sort()function that is provided.

它们是相同的,实际上文档页面$orderby实际上主要讨论所sort()提供的功能。

These query modifiersthat allow you to add sections of a query on without using the functional accessors do exist but there is a bug mixing these two together so I would recommend you pick either the query modifiers or the functional methods and stick to that option.

这些允许您在不使用功能访问器的情况下添加查询部分的查询修饰符确实存在,但是存在将这两者混合在一起的错误,因此我建议您选择查询修饰符或功能方法并坚持使用该选项。

In attempting to provide example code I have also found out one other thing when I looked at your question again. You provide:

在尝试提供示例代码时,当我再次查看您的问题时,我还发现了另一件事。你提供:

db.collection.findOne({"$query":{},"$orderby":{ "_id": -1 }}) 

But it is good to note that:

但值得注意的是:

db.collection.findOne({}).sort({ "_id":-1})

Actually produces:

实际产生:

2014-07-31T04:59:50.183-0700 TypeError: Object [object Object] has no method 'sort'

2014-07-31T04:59:50.183-0700 TypeError: Object [object Object] 没有方法“排序”

and as you can see here by my test data set:

正如您在我的测试数据集中看到的:

> db.rooms.find()
{ "_id" : ObjectId("53ad206e1d8f2d8351182830"), "id" : 1, "from" : ISODate("2014-06-26T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad276f1d8f2d8351182831"), "id" : 1, "from" : ISODate("2014-06-24T00:00:00Z"), "to" : ISODate("2014-07-01T00:00:00Z") }
{ "_id" : ObjectId("53ad28ad1d8f2d8351182832"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad28c61d8f2d8351182833"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-07-03T00:00:00Z") }
{ "_id" : ObjectId("53ad29971d8f2d8351182834"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-06-21T00:00:00Z") }

the answer is actually correct:

答案实际上是正确的:

> db.rooms.findOne({ "$query":{}, "$orderby":{ "_id": -1 }})
{
        "_id" : ObjectId("53ad29971d8f2d8351182834"),
        "id" : 1,
        "from" : ISODate("2014-06-20T00:00:00Z"),
        "to" : ISODate("2014-06-21T00:00:00Z")
}

So it is interesting to note that query modifiers are supported by findOnewhere as functional accessors are not, which could be a reason to use query modifiers instead.

所以有趣的是,findOnewhere支持查询修饰符,而函数访问器不支持,这可能是使用查询修饰符的原因。