Mongodb Find的退货单据顺序 [英] Mongodb find's returning document order

查看:63
本文介绍了Mongodb Find的退货单据顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合,该集合的字段名为"date"(也已建立索引).它包含值yyyymmdd(例如:20140731、20140730、20140729 ...)

I have a collection with an field named "date" (also indexed). It holds the values yyyymmdd (example: 20140731, 20140730, 20140729...)

文档根据日期降序存储.因此,该集合的第一个文档为20140731.

The documents are stored in descending order based on date. So the collection has 20140731 as the first document.

当我将find命令与过滤器{$ gte:20140720,$ lte:20140731}一起使用时,mongodb以日期"字段的升序返回查询.

When I use the find command with filters {$gte : 20140720, $lte : 20140731}, mongodb returns back the query in ascending order of "date" field.

我知道我可以使用sort,但是如何使mongodb根据创建顺序返回结果?

I know I can use sort, but how do I get mongodb to return results based on the order it was created?

谢谢!

推荐答案

文档以自然顺序存储

文档根据日期降序存储.因此,该集合的第一个文档为20140731.

The documents are stored in descending order based on date. So the collection has 20140731 as the first document.

除非您使用的是封顶的收藏夹,否则不能保证磁盘上文档的顺序(也称为自然顺序)

Unless you are using a capped collection, there is no guarantee for the ordering of documents on disk (also referred to as natural order).

文档的删除和移动(当文档超出其分配的记录空间时)会在空闲列表上创建空间,以供重用.

Document deletions and moves (when a document outgrows its allocated record space) create space on the free list which will be reused.

这是一个简单的示例,应该在mongo shell中对此进行演示:

Here's a quick example which should demonstrate this in the mongo shell:

// Start with an empty database & collection
use demodb; db.dropDatabase(); db.order.drop()

// Add some test data
for (i=0; i<1000; i++) {
    db.order.insert({'i': i})
}

// Looks like insertion order! (0..9)
db.order.find({}).limit(10);

// Pause 5s for effect :)
sleep(5000);

// Remove half the entries
db.order.remove({ i: { $lt: 500 }})

// Re-add the missing entries
for (i=0; i<500; i++) {
    db.order.insert({'i': i})
}

// Not the entries you expected .. space from deleted records was reused
db.order.find({}).limit(10)

// Clean up demodb
db.dropDatabase()

结果顺序

当我将find命令与过滤器{$ gte:20140720,$ lte:20140731}一起使用时,mongodb以日期"字段的升序返回查询.

When I use the find command with filters {$gte : 20140720, $lte : 20140731}, mongodb returns back the query in ascending order of "date" field.

如果将索引用于查询,则按在索引中找到文档的顺序返回文档.在构造常见查询的索引时,应利用此优势(请参阅:使用索引对查询结果进行排序).

If an index is used for a query, the documents are returned in the order they are found in the index. You should take advantage of this when constructing your indexes for common queries (see: Use Indexes to Sort Query Results).

FYI,可以使用简单的索引(例如,在{date:1}上)返回按升序或降序排序的结果.

FYI, a simple index (eg. on {date:1}) can be used to return the results sorted in either ascending or descending order.

如果您为_id使用MongoDB的默认ObjectID ,则您可以按{ _id: 1 }排序,以近似插入顺序,因为ObjectID的前4个字节包含时间戳.如果要使用它对基于date和近似插入顺序的查询进行排序,则应确保在{date:1, _id:1}上建立索引.

If you are using MongoDB's default ObjectIDs for _id, you can sort by { _id: 1 } to approximate insertion order since the first 4 bytes of the ObjectID incorporate a timestamp. If you wanted to use this for sorting a query based on date and approximate insertion order you would ensure an index on {date:1, _id:1}.

请注意,ObjectID通常是由客户端驱动程序生成的,因此,如果您的应用服务器上存在时钟漂移(或者_id是在插入文档之前的某个时间创建的),则ObjectID可能不会严格反映插入顺序"如服务器所见.如果插入顺序的准确性非常重要,通常可以在服务器端生成_id(方法因驱动程序而异).

Note that ObjectIDs are typically generated by the client driver, so if you have clock drift on your app servers (or the _id is created some time before the document is inserted) the ObjectIDs may not strictly reflect "insertion order" as seen by the server. If accuracy of insertion order is highly important, it is generally possible to generate the _id on the server-side (approach varies depending on the driver).

这篇关于Mongodb Find的退货单据顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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