问题
I've a collection called events
. Sample document is like this.
I want to select documents with in date range of '2019-01-11'
to '2019-11-27'
.
What I've done is this. But this seems not working.
How do I achieve this using MongoDB
Mongoose
?
$match: {
createdAt: {
$gte: {
$dayOfYear: '2019-01-11'
},
$lte: {
$dayOfYear: '2019-11-27'
}
}
}
回答1:
Use the Date()
object instead. I am not sure if this affects the timezone as well.
db.collection.find({
"createdAt": {
"$gte": new Date('2019, 1, 11'),
"$lt" : new Date(2019, 11, 27)
}
})
来源:https://stackoverflow.com/questions/59066544/mongodb-mongoose-select-documents-between-a-date-range