I am trying to $match my data after $group but it does not work. I have db with jobs. Every job has {id, batchId(a few jobs can belong to one batch), createdAt, finishedAt}.
I cant understand why I am getting empty result. I need to get the groups of the jobs with the same batchId that were created before CreatedAtTo parameter. For saving date I used unix timestamp (so it is an integer)
My solution:
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}
If I comment $match part it works, but not all together. And tried to perform in robo 3t. It works as well
db.getCollection('jobs').aggregate(
[
{$group:{
"_id":"$batchId",
btime: { $max: "$createdAt"},
bItems: { $push: "$$CURRENT" }
}},
{$match:{"btime": {"$lte": 10}}},
{$sort:{"btime": -1}},
{$limit:1},
])
Am I missing something ?
It should work, make sure all your field is a valid date or use $toDate
db.collection.aggregate([
{
$group: {
_id: "$batchid",
btime: {
$max: "$createdAt"
},
doc: {
$push: "$$ROOT"
}
}
},
{
$match: {
btime: {
$gt: "2021-07-01T11:23:25.184Z"
}
}
}
])