我試圖在 $group 之后 $match 我的數(shù)據(jù),但它不起作用。我有工作的數(shù)據(jù)庫。每個作業(yè)都有 { id,batchId(幾個作業(yè)可以屬于一個批次),createdAt, finishedAt}。我不明白為什么我得到的結果是空的。我需要獲取在 CreatedAtTo 參數(shù)之前創(chuàng)建的具有相同 batchId 的作業(yè)組。為了保存日期,我使用了 unix 時間戳(所以它是一個整數(shù))我的解決方案:group := bson.M{ "$group": bson.M{ "_id": "$batchid", "btime": bson.M{"$max": "$createdAt"}, "doc": bson.M{"$push": "$$ROOT"}, },}match := bson.M{ "$match": bson.M{ "btime": bson.M{ "$lte": CreatedAtTo}, },}sort := bson.M{"$sort": bson.M{"btime": -1}}limit := bson.M{"$limit": 1}pipeline := []bson.M{group, match, sort, limit}如果我評論 $match 部分它有效,但不是全部。并嘗試在robo 3t中表演。它也有效 db.getCollection('jobs').aggregate( [ {$group:{ "_id":"$batchId", btime: { $max: "$createdAt"}, bItems: { $push: "$$CURRENT" } }}, {$match:{"btime": {"$lte": 10}}}, {$sort:{"btime": -1}}, {$limit:1}, ])我錯過了什么嗎?
1 回答

慕容森
TA貢獻1853條經驗 獲得超18個贊
它應該有效,確保您所有的字段都是有效日期或使用$toDate
db.collection.aggregate([
{
$group: {
_id: "$batchid",
btime: {
$max: "$createdAt"
},
doc: {
$push: "$$ROOT"
}
}
},
{
$match: {
btime: {
$gt: "2021-07-01T11:23:25.184Z"
}
}
}
])
mongo游樂場
- 1 回答
- 0 關注
- 111 瀏覽
添加回答
舉報
0/150
提交
取消