我在mongoDB中有一条记录,如下所示。需要查询以使用startTime获得前一天的结果。
{
"_id": ObjectId("5c08d195d38e4040788c789a"),
"jobName": "test-ci-build",
"build": "474",
"buildURL": "https://example.com/test-ci-build/474/",
"result": "SUCCESS",
"startTime": "2018-12-06T05:42:22+0000",
"duration": "1 hr 54 min",
"startTimeInMillis": 1544074942061,
"triggeredBy": "Started by timer",
"commit": "b8a04837f1c285e6d9d8852af5801419acd047cb",
"date": ISODate("2018-12-06T07:36:53.045Z")
}
答案 0 :(得分:0)
在startTime字段上使用$lt或$lte MongoDB运算符,您可以获得所需的数据。
您可以使用:
db.collection.find({startTime:{$lte: "2018-12-06T05:42:22+0000"}})
或
db.collection.find({startTime:{$lte: ISODate("2018-12-06T05:42:22+0000")}})
考虑为您的“ startDate db.collection.aggregate([
{
$addFields: {
yesterdays_date: {
$subtract: [
new Date().getTime(),
86400000
]
}
}
},
{
$addFields: {
convertedDate: {
$toDate: "$yesterdays_date"
}
}
},
{
$addFields: {
"dateComp": {
"$cmp": [
"$date",
"$convertedDate"
]
}
}
},
{
$match: {
"dateComp": -1
}
},
{
$project: {
yesterdays_date: 0,
convertedDate: 0,
dateComp: 0
}
}
])