MongoDB和Golang – $group和$match

huangapple go评论136阅读模式
英文:

MongoDb and Golang - $group and $match

问题

我正在尝试在$group之后匹配我的数据,但它不起作用。
我有一个带有jobs的数据库。
每个工作都有{idbatchId(几个工作可以属于一个批次),createdAt,finishedAt}。

我不明白为什么我得到了空结果。我需要获取具有相同batchId的作业组,这些作业是在CreatedAtTo参数之前创建的。
为了保存日期,我使用了Unix时间戳(因此它是一个整数)。

我的解决方案:

  1. group := bson.M{
  2. "$group": bson.M{
  3. "_id": "$batchid",
  4. "btime": bson.M{"$max": "$createdAt"},
  5. "doc": bson.M{"$push": "$$ROOT"},
  6. },
  7. }
  8. match := bson.M{
  9. "$match": bson.M{
  10. "btime": bson.M{
  11. "$lte": CreatedAtTo},
  12. },
  13. }
  14. sort := bson.M{"$sort": bson.M{"btime": -1}}
  15. limit := bson.M{"$limit": 1}
  16. pipeline := []bson.M{group, match, sort, limit}

如果我注释掉$match部分,它可以工作,但是全部一起就不行。我尝试在robo 3t中执行它,它也可以工作。

  1. db.getCollection('jobs').aggregate(
  2. [
  3. {$group:{
  4. "_id":"$batchId",
  5. btime: { $max: "$createdAt"},
  6. bItems: { $push: "$$CURRENT" }
  7. }},
  8. {$match:{"btime": {"$lte": 10}}},
  9. {$sort:{"btime": -1}},
  10. {$limit:1},
  11. ])

我是否漏掉了什么?

英文:

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:

  1. group := bson.M{
  2. "$group": bson.M{
  3. "_id": "$batchid",
  4. "btime": bson.M{"$max": "$createdAt"},
  5. "doc": bson.M{"$push": "$$ROOT"},
  6. },
  7. }
  8. match := bson.M{
  9. "$match": bson.M{
  10. "btime": bson.M{
  11. "$lte": CreatedAtTo},
  12. },
  13. }
  14. sort := bson.M{"$sort": bson.M{"btime": -1}}
  15. limit := bson.M{"$limit": 1}
  16. 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

  1. db.getCollection('jobs').aggregate(
  2. [
  3. {$group:{
  4. "_id":"$batchId",
  5. btime: { $max: "$createdAt"},
  6. bItems: { $push: "$$CURRENT" }
  7. }},
  8. {$match:{"btime": {"$lte": 10}}},
  9. {$sort:{"btime": -1}},
  10. {$limit:1},
  11. ])

Am I missing something ?

答案1

得分: 1

这是要翻译的内容:

应该可以工作,确保你的所有字段都是有效的日期,或者使用$toDate函数。

  1. db.collection.aggregate([
  2. {
  3. $group: {
  4. _id: "$batchid",
  5. btime: {
  6. $max: "$createdAt"
  7. },
  8. doc: {
  9. $push: "$$ROOT"
  10. }
  11. }
  12. },
  13. {
  14. $match: {
  15. btime: {
  16. $gt: "2021-07-01T11:23:25.184Z"
  17. }
  18. }
  19. }
  20. ])

mongoplayground

英文:

It should work, make sure all your field is a valid date or use $toDate

  1. db.collection.aggregate([
  2. {
  3. $group: {
  4. _id: "$batchid",
  5. btime: {
  6. $max: "$createdAt"
  7. },
  8. doc: {
  9. $push: "$$ROOT"
  10. }
  11. }
  12. },
  13. {
  14. $match: {
  15. btime: {
  16. $gt: "2021-07-01T11:23:25.184Z"
  17. }
  18. }
  19. }
  20. ])

mongoplayground

huangapple
  • 本文由 发表于 2022年3月15日 15:25:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/71478338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定