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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
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() );
})
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.