获取所有文档,如果相等(相似)则获取后面的文档

  • 本文关键字:获取 文档 相似 如果 mongodb
  • 更新时间 :
  • 英文 :


有一个用于体育的解析器。这是一个循环。通过一些逻辑,体育赛事被添加到数据库中。在每个解析周期中,可以向数据库中添加一个体育事件,但也可能不添加)

我需要从最后两个解析周期中获得所有体育事件。但是,如果两个周期都有一项体育赛事,那么只从最后一个周期开始。这就是问题所在。示例文件:

{
"command1": "A",
"command2": "B",
"parseCount": 0
},
{
"command1": "A",
"command2": "B",
"parseCount": 1
},
{
"command1": "A",
"command2": "B",
"parseCount": 2
},
{
"command1": "C",
"command2": "D",
"parseCount": 1
},
{
"command1": "E",
"command2": "F",
"parseCount": 2
},

因此,我应该从上面的列表中得到最后3个文档。文档也有字段:match time和ObjectId https://mongoplayground.net/p/-9gz4zOnudW

如果我理解正确,你可以先$sort,然后得到$first对象到$group,像这样:

我已经使用了$first: $$ROOT,但你可以使用$first: value从对象的每个值,如果你想。

这个查询:

  • 首先按parseCount排序,以获得第一个位置的较大值。
  • 然后$group按两个条件,获得第一个对象(按顺序是较高的)
  • 和使用$project得到你想要的输出值。
db.collection.aggregate([
{
"$sort": {
"parseCount": -1
}
},
{
"$group": {
"_id": {
"command1": "$command1",
"command2": "$command2"
},
"object": {
"$first": "$$ROOT"
}
}
},
{
"$project": {
"_id": "$object._id",
"command1": "$object.command1",
"command2": "$object.command2",
"parseCount": "$object.parseCount"
}
}
])

例子

查询
  • 它像2个查询,但可以变成1查找
  • facet可以使用并做2组,但将限制为最大16mb
  • 使用查找查找集合的最大解析(mongodb自动优化它,所以管道查找将只运行一次不是每个收集文档,至少在我过去测试时发生过这种情况)
  • 我们只保留最后两个解析,例如,如果max=3我们保留parseCount=3parseCount=2,我们也只保留parseCount>0,你在操场上有那个过滤器,如果你不需要它删除它。
  • command1,command2,只保留最大的parseCount,你说我们只保留最新的,如果我们有超过1
  • 项目恢复文档结构,matchTime_id也被保留因为你说你也有

此处测试代码

db.collection.aggregate([
{
"$lookup": {
"from": "collection",
"pipeline": [
{
"$group": {
"_id": null,
"maxParse": {
"$max": "$parseCount"
}
}
}
],
"as": "result"
}
},
{
"$set": {
"maxParses": {
"$let": {
"vars": {
"v0": {
"$arrayElemAt": [
"$result",
0
]
}
},
"in": "$$v0.maxParse"
}
}
}
},
{
"$unset": [
"result"
]
},
{
"$match": {
"$expr": {
"$and": [
{
"$gt": [
"$parseCount",
0
]
},
{
"$gte": [
"$parseCount",
{
"$subtract": [
"$maxParses",
1
]
}
]
}
]
}
}
},
{
"$group": {
"_id": {
"command1": "$command1",
"command2": "$command2"
},
"maxParseCount": {
"$max": {
"parseCount": "$parseCount",
"matchTime": "$matchTime",
"id": "$_id"
}
}
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$_id",
"$maxParseCount",
"$$ROOT"
]
}
}
},
{
"$project": {
"command1": 1,
"command2": 1,
"parseCount": 1,
"matchTime": 1,
"_id": "$id"
}
}
])

根据你的数据解释什么是查询

  • 将查找maxParse=2(查找执行此操作)
  • 第一个文档将被过滤掉,因为只保留2.1个解析它有0
  • 其余的按command1,command2分组"A","B"有2个文档(_id=2,_id=3),但只有_id=3会通过,因为它有最大的parseCount
[
{
"_id": 1,
"command1": "A",
"command2": "B",
"parseCount": 0,
"matchTime": 1
},
{
"_id": 2,
"command1": "A",
"command2": "B",
"parseCount": 1,
"matchTime": 2
},
{
"_id": 3,
"command1": "A",
"command2": "B",
"parseCount": 2,
"matchTime": 3
},
{
"_id": 4,
"command1": "C",
"command2": "D",
"parseCount": 1,
"matchTime": 4
},
{
"_id": 5,
"command1": "E",
"command2": "F",
"parseCount": 2,
"matchTime": 5
}
]

结果

[
{
"_id": 3,
"command1": "A",
"command2": "B",
"matchTime": 3,
"parseCount": 2
},
{
"_id": 4,
"command1": "C",
"command2": "D",
"matchTime": 4,
"parseCount": 1
},
{
"_id": 5,
"command1": "E",
"command2": "F",
"matchTime": 5,
"parseCount": 2
}
]

最新更新