Ray*_*ahn 3 mongoose mongodb node.js
我试图根据日期字段查询集合.我的收藏品有一个日期+时间戳类型的字段.但是我想忽略时间戳并只使用日期部分.该字段为:"enddate":ISODate("2014-10-10T07:00:00Z").我使用以下查询:
Camps.findOne(
{ $and: [ {status: 1} , {camp_id: pCampID} , {$or: [ {enddate: null}, {enddate: {$gte: new Date()} } ] } ] },...
Run Code Online (Sandbox Code Playgroud)
但是date(new Date())被转换为UTC日期,这导致查询不返回所有文档.
任何帮助是极大的赞赏.
一种方法是使用aggregation framework并利用date aggregation operators.例如:
db.camps.aggregate(
[
// Perform the initial match to filter docs by 'status' and 'camp_id'
{
$match:
{
status: 1,
camp_id: pCampID
}
},
// Extract the year, month and day portions of the 'enddate'
{
$project:
{
year: { $year: "$enddate" },
month: { $month: "$enddate" },
day: { $dayOfMonth: "$enddate" },
status: 1,
camp_id: 1
}
},
// Filter by date - Replace hard-coded values with values you want
{
$match:
{
year: { $gte: 2014 },
month: { $gte: 10 },
day: { $gte: 10 }
}
}
]
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2533 次 |
| 最近记录: |