简体   繁体   English

使用pymongo获取具有排序和限制的请求结果

[英]Get results of request with sort and limit using pymongo

Let's take this simple collection col with 2 documents: 让我们以2个文档作为这个简单的集合col

{
    "_id" : ObjectId("5ca4bf475e7a8e4881ef9dd2"),
    "timestamp" : 1551736800,
    "score" : 10
}
{
    "_id" : ObjectId("5ca4bf475e7a8e4881ef9dd3"),
    "timestamp" : 1551737400,
    "score" : 12
}

To access the last timestamp (the one of the second document), I first did this request 要访问上一个时间戳(第二个文档中的一个),我首先执行了此请求

a = db['col'].find({}).sort("_id", -1)

and then a[0]['timestamp'] 然后a[0]['timestamp']

But as there will be a lot of documents in this collection, i think that it would be more efficient to request only the last one with the limit function, like 但是由于此集合中将有很多文档,我认为只请求带有limit函数的最后一个文档会更有效,例如

a = db['col'].find({}).sort("_id", -1).limit(1)

and then 接着

for doc in a:
    lastTimestamp = doc['timestamp']

as there will be only one, i can declare the variable inside the loop. 因为只有一个,所以我可以在循环内声明变量。

So three questions : 所以三个问题:

  • Do i have to worry about memory / speed issues if i continue to use the first request and get the first element in the dic ? 如果我继续使用第一个请求并获得dic中的第一个元素,是否需要担心内存/速度问题?
  • Is there a smarter way to access the first element of the cursor instead of using a loop, when using the limit request ? 使用限制请求时,是否有更聪明的方法来访问游标的第一个元素而不是使用循环?
  • Is there another way to get that timestamp that i don't know ? 还有另一种我不知道的时间戳获取方式吗?

Thanks ! 谢谢 ! Python 3.6 / Pymongo 3.7 Python 3.6 / Pymongo 3.7

If you are using any field with an unique index in the selection criteria, you should use find_one method which will return the only document that matches your query. 如果在选择条件中使用具有唯一索引的任何字段,则应使用find_one方法,该方法将返回唯一与查询匹配的文档。

That being said, the find method returns a Cursor object and does not load the data into memory. 就是说, find方法返回一个Cursor对象,并且不会将数据加载到内存中。

You might get a better performance if you where using a filter option. 如果使用过滤器选项,则可能会获得更好的性能。 Your query as it is now will do a collection scan. 您现在的查询将进行集合扫描。

if you are not using a filter, and want to retrieve the last document, then the clean way is with the Python built-in next function. 如果您不使用过滤器,并且想检索最后一个文档,那么干净的方法是使用Python内置的next函数。 You could also use the next method. 您也可以使用next方法。

cur = db["col"].find().sort({"_id": -1}).limit(1):
with cur:
    doc = next(cur, None) # None when we have empty collection.

find()。sort()是如此之快,不必担心速度,它是访问游标第一个元素的最佳途径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM