[英]How can I retrieve the most recently added documents in mongodb-mongoose without using sort?
I have a large mongodb collection where each document represents a simple log message that occurs at at set time interval (say 1 minute). 我有一个大型的mongodb集合,其中每个文档代表一个简单的日志消息,该消息以设置的时间间隔(例如1分钟)出现。 Whenever I do a
Find
using Mongoose, I get the entire contents of the DB in the order which they were added. 每当我使用Mongoose进行
Find
,我都会按添加顺序获得数据库的全部内容。 I am looking for a query where I can get the last 10 added records without doing a sort on a timestamp. 我正在寻找一个查询,我可以在不进行时间戳排序的情况下获得最后添加的10条记录。 Below is a sample of my of my documents:
以下是我的文件样本:
[ { _id: 58bd2dcd51d1169260ccbdac,
contentID: 1,
clicks: 50,
views: 61 },
{ _id: 58bd43d40fda0d25b0c6724e,
'contentID: ': 1,
clicks: 45,
views: 72 },
{ _id: 58bd54ae2d67e820143eddda,
'contentID: ': 1,
clicks: 25,
views: 32 }]
Unfortunately I believe you will have to use sort in your query. 不幸的是,我相信您将不得不在查询中使用排序。
example.find()sort.({"_id": -1}).limit(10)
to make it easier, create a variable and then call the variable 为了简化操作,创建一个变量,然后调用该变量
var q = db.example.find()sort.({"_id": -1}).limit(10);
q.exec(function() ...},{})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.