我想使用mongodb的_id检索在特定日期插入的值 [英] I want to retrieve values inserted on particular date using _id of mongodb

查看:94
本文介绍了我想使用mongodb的_id检索在特定日期插入的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索在特定日期插入的值。这可能使用mongodb_id字段吗?因为这包含嵌入的日期时间。我想通过使用任何应用程序来修复mongodb shell中的值。

I want to retrieve values inserted on particular date. Is this possible using mongodb "_id" field? as this contains embedded date time. I want to retieve values in mongodb shell not by using any application.

推荐答案

虽然它是确实,ObjectId部分基于时间戳,通常这是一个客户端库操作,用于从ObjectId值中提取该日期。

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.

你可以通过 $的JavaScript评估来完成此操作其中 ,但需要扫描整个集合,因此效率不高:

You can do this with the JavaScript evaluation of $where, but it will need to "scan" the entire collection, so is not very efficient:

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

基本上比较一下,看看 ObjectId 是在山姆上创建的提供日期的日期。其他日期数学或方法适用于其他区间。

That will basically compare to see if the ObjectId was created on the same day as the date provided. Other date math or methods apply to other intervals.

这篇关于我想使用mongodb的_id检索在特定日期插入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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