简体   繁体   English

我想使用mongodb的_id检索在特定日期插入的值

[英]I want to retrieve values inserted on particular date using _id of mongodb

I want to retrieve values inserted on particular date. 我想检索在特定日期插入的值。 Is this possible using mongodb "_id" field? 使用mongodb“ _id”字段可以做到这一点吗? as this contains embedded date time. 因为它包含嵌入的日期时间。 I want to retieve values in mongodb shell not by using any application. 我不希望通过使用任何应用程序来重新获得mongodb shell中的值。

While it is true that the ObjectId is based on a "timestamp" in part, generally this is a "client" library operation to "extract" this date from the ObjectId value. 虽然确实ObjectObjectId部分基于“时间戳”,但通常这是一个“客户端”库操作,用于从ObjectId值“提取”该日期。

You can do this with the JavaScript evaluation of $where , but it will need to "scan" the entire collection, so is not very efficient: 您可以使用$where的JavaScript评估来完成此操作,但是它将需要“扫描”整个集合,因此效率不是很高:

 db.collection.find(function() {
     return (
        ( this._id.getTimestamp().valueOf() - 
          this._id.getTimestamp().valueOf() % ( 1000 * 60 * 60 * 24 ) )
        == new Date("2014-07-14").valueOf() );
 })

That will basically compare to see if the ObjectId was created on the same day as the date provided. 基本上可以进行比较,以查看是否在提供日期的同一天创建了ObjectId Other date math or methods apply to other intervals. 其他日期数学或方法也适用于其他间隔。

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

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