我有一个这样的数组

[
{
    "name": "CAMP-1",
    "status": "incomplete",
    "version": 3,
  },
  {
    "name": "CAMP-1",
    "status": "complete",
    "version": 2,
  },
  {
    "name": "CAMP-1",
    "status": "complete",
    "version": 1,
  },
  {
    "name": "CAMP-2",
    "status": "complete",
    "version": 2,
  },
{
    "name": "CAMP-2",
    "status": "incomplete",
    "version": 1,
  }
]

如果最新版本的状态为不完整,则应返回最新的不完整版本和完整版本.

如果最新版本的状态为"完成",则只应返回该版本.

我试着按名字和状态分组,这给出了最新版本的不完整和完整对象

db.collection.aggregate({
  "$sort": {
    "version": -1
  }
},
{
  "$group": {
    "_id": {
      "content": "$name",
      "status": "$status"
    },
    "status": {
      "$first": "$$ROOT"
    },
    "content": {
      "$first": "$$ROOT"
    }
  }
},
{
  "$replaceRoot": {
    "newRoot": "$content"
  }
})

我得到的结果是

[
{
    "name": "CAMP-1",
    "status": "incomplete",
    "version": 3,
  },
{
    "name": "CAMP-1",
    "status": "complete",
    "version": 2,
  },
  {
    "name": "CAMP-1",
    "status": "complete",
    "version": 1,
  },
{
    "name": "CAMP-2",
    "status": "incomplete",
    "version": 2,
  }
]

但预期的输出是

[
{
    "name": "CAMP-1",
    "status": "incomplete",
    "version": 3,
  },
{
    "name": "CAMP-1",
    "status": "complete",
    "version": 2,
  },
{
    "name": "CAMP-2",
    "status": "complete",
    "version": 2,
  }
]

有谁能帮助我们根据状态过滤数据吗?

推荐答案

查询

  • 按姓名分组
  • 找到最大完整版本
  • 过滤以保持已完成的版本相同,未完成的版本更大
  • 松开并更换根部

*For example for 1 name, if you have incomplete version 5 6 7 3 and complete 2 3 4 , you will get 5 6 7 incompletes and 4 complete.
If its not what you want exactly maybe with small changes you can get what you need.

Playmongo(看看每个阶段都把鼠标放在了它的末端)

aggregate(
[{"$group": {"_id": "$name", "docs": {"$push": "$$ROOT"}}},
 {"$set": 
   {"max-complete": 
     {"$let": 
       {"vars": 
         {"c": 
           {"$filter": 
             {"input": "$docs",
              "cond": {"$eq": ["$$this.status", "complete"]}}}},
        "in": {"$max": "$$c.version"}}}}},
 {"$set": 
   {"docs": 
     {"$filter": 
       {"input": "$docs",
        "cond": 
         {"$or": 
           [{"$and": 
               [{"$gt": ["$$this.version", "$max-complete"]},
                 {"$eq": ["$$this.status", "incomplete"]}]},
             {"$and": 
               [{"$eq": ["$$this.version", "$max-complete"]},
                 {"$eq": ["$$this.status", "complete"]}]}]}}}}},
 {"$unwind": "$docs"},
 {"$replaceRoot": {"newRoot": "$docs"}}])

Mongodb相关问答推荐

执行聚合以消除重复并获得唯一计数

使用Go的Mongo驱动程序,从MongoDB文档中获取元素并更新其值需要帮助

spring-data-mongodb 上的 TopN 聚合

MongoDB 按 created_at 月聚合

@DynamicPropertySource 未被调用(Kotlin、Spring Boot 和 TestContainers)

如何修改 mongodb 中嵌套对象数组中的字段名称/键?

$eq 的目的是什么

MongoDB获取聚合查询的executionStats

MongoDB:设置 Windows 服务

Select 匹配mongodb中两个字段的concat值的记录

使用 mgo 存储嵌套 struct

如何使用 c# 2.0 驱动程序将数据插入到 mongodb 集合中?

MongoDB:多个 $elemMatch

MongoDB Java API:put() 与 append()

如何使用 mgo 和 Go 查询 MongoDB 的日期范围?

没有参考选项的mongoose填充字段

有人在 Google App Engine 上try 过 MongoDB 吗?

MongoDB MapReduce - 发出一个键/一个值不调用reduce

RoboMongo:不显示所有文档

验证 MongoCredential 的异常和未分类的 Mongo Db 异常