0

我在我的 mongodb 中重新发送了一些大量记录 这是示例

db.logins.find().pretty()

        "cust_id" : "testuser",
        "error" : "no error"

        "cust_id" : "testuser",
        "error" : "unable to connect"

    "cust_id" : "sam",
        "error" : "no error"

        "cust_id" : "sdert",
        "error" : "unable to connect"

当我这样做db.logins.find().pretty()时,它会显示过去的记录(旧的)

我需要连续执行命令it才能看到它们

我的问题是,我最初如何查看最新记录db.logins.find().pretty()

4

3 回答 3

3

您可以使用排序。也就是说,如果您有一个包含日期对象的字段,您可以使用它对结果进行排序。那是,

 db.logins.find().sort({date_field: -1}).pretty()

-1表示记录将按降序排序。

如果有任何帮助,您可以尝试按自然顺序排序

 db.logins.find().sort({$natural: -1}).pretty()

但是,这并不能保证排序的结果将匹配插入顺序。在日期字段上排序仍然会更好。

于 2013-05-20T07:08:43.157 回答
0

您可以使用 MongoDB 为您生成的 _id 字段 - 前四个字节是时间戳,因此按降序排序将为您提供反向插入顺序。

使用 _id 和 $natural 之间的区别在于后者按磁盘顺序提供它们,并且可能无法保证您的记录已连续写入磁盘上,其中 _id 字段的升序得到保证。

db.logins.find().sort({_id:-1})

此外,_id 上有一个索引, sort 它用于排序以避免性能损失排序。

于 2013-05-20T12:32:03.497 回答
0

您也可以在这里使用 $natural ,这将产生几乎相同的效果(不完全是由于分布式环境):

http://docs.mongodb.org/manual/reference/operator/natural/

db.logins.find().sort({$natural:-1})
于 2013-05-20T07:27:02.720 回答