如果您想选择记录,请使用日期范围:
db.collection.find({
created: { "$gte": new Date("2013-05-27"), "$lt": new Date("2013-05-28") }
})
这将选择两个日期之间所有包含的小时、分钟等。
So you should尝试使用日期值而不是强制转换为字符串。
如果你想用它来进行聚合或其他操作need结果仅采用一天的格式,然后使用$project http://docs.mongodb.org/manual/reference/operator/aggregation/project/和日期运营商 http://docs.mongodb.org/manual/reference/operator/aggregation-date/:
db.collection.aggregate([
// Still match on the normal date forms, this time whole month
{ "$match": {
created: {
"$gte": new Date("2013-05-01"),
"$lt": new Date("2013-05-31")
}
}},
// Project the date
{ "$project": {
"date": {
"year" : { "$year" : "$created" },
"month" : { "$month" : "$created" },
"day": : { "$dayOfMonth": "$created" }
},
"title": 1
}},
// Group on day and title
{ "$group": {
"_id": {
"date" : "$date",
"title" : "$title"
},
"count": { "$sum": 1 }
}},
// Sort by date
{ "$sort": {
"_id.date.year": 1,
"_id.date.month": 1,
"_id.date.day": 1,
}},
// Project nicer dates and document
{ "$project": {
"_id": 0,
"date": { "$concat": [
{ "$substr": [ "$_id.date.year", 0, 4 ] },
"-",
{ "$substr": [ "$_id.date.month", 0, 2 ] },
"-",
{ "$substr": [ "$_id.date.day", 0, 2 ] }
]},
"title": "$_id.title",
"count": 1
}}
])