根据状态过滤结果

发布于 2025-01-21 13:49:59 字数 1504 浏览 0 评论 0原文

我有这样的数组,

[
{
    "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,
  }
]

任何人都可以帮助如何根据状态过滤数据吗?

I have an array like this

[
{
    "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,
  }
]

if the status of latest version is incomplete then both the latest incomplete and complete versions should be returned.

if the status of the latest version is complete then only that version should be returned.

I tried to group by name and status which gives the latest version of incomplete and complete object

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

The output which I get is

[
{
    "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,
  }
]

But the expected output is

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

Can anyone please help on how to filter the data based on status?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2025-01-28 13:49:59

查询

  • 组按名称
  • 找到最大完整版本
  • 过滤器,以保持相同版本的完成,并使用更大版本
  • 放松并替换root

*,例如1个名称,如果您有不完整的版本5 6 7 3并完成2 3 4 ,您将获得5 6 7的未完成和4个完成。
如果不是您想要的,也许可以通过微小的更改来获得所需的东西。

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

Query

  • group by name
  • find the max-complete version
  • filter to keep the completed with the same version and the uncompleted with bigger version
  • unwind and replace root

*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 (to see what each stage does put the mouse on its end)

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"}}])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文