how can I write filter which results docs created today.I know ObjectId has timestamp. I tried this :
db.doc.find({_id : { $gte : ObjectId().getTimestamp().getTime() }}
Can I write
db.doc.find({'_id.getTimestamp().getTime()' : { $gte : ObjectId().getTimestamp().getTime() }}
The current date in the MongoDB query The following two ways return date as a string or as a date object: The Date() will return the current date as a string. The new Date() will return the current date a date object and MongoDB cover the Date object with the ISODate.
The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.
Connect to a database using the getDatabase() method. Get the object of the collection from which you want to retrieve the documents, using the getCollection() method. Retrieve the iterable object containing all the documents of the current collection by invoking the find() method.
find() in MongoDB? The statement db. collection. find() returns the cursor of Result Set of a query by which you can iterate over the result set or print all documents.
Try the following (based on this answer). This returns all documents created since the given date. So it covers todays entries as well.
db.doc.find({_id : { $gt : ObjectId(Math.floor(new Date('2014/01/30')/1000).toString(16)+"0000000000000000") }})
If you don't like to enter the date as string, you can create it via Objects, but it gets a little bit ugly:
db.doc.find({_id : { $gt : ObjectId(Math.floor(new Date(new Date().getFullYear()+'/'+(new Date().getMonth()+1)+'/'+new Date().getDate())/1000).toString(16)+"0000000000000000") }})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With