MongoDB和Golang – $group和$match

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

MongoDb and Golang - $group and $match

问题

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

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

我的解决方案:

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},
    ])

我是否漏掉了什么?

英文:

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 ?

答案1

得分: 1

这是要翻译的内容:

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

db.collection.aggregate([
  {
    $group: {
      _id: "$batchid",
      btime: {
        $max: "$createdAt"
      },
      doc: {
        $push: "$$ROOT"
      }
    }
  },
  {
    $match: {
      btime: {
        $gt: "2021-07-01T11:23:25.184Z"
      }
    }
  }
])

mongoplayground

英文:

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"
      }
    }
  }
])

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:

确定