问题
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() }}
回答1:
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") }})
来源:https://stackoverflow.com/questions/21464886/query-mongodb-to-return-documents-created-today