MongoDB聚合以找到具有两个布尔值的记录

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

Mongodb aggregate to find record that has both value of a boolean

问题

"I have these documents:

[
    {
        "id": 1,
        "condition": true
    },
    {
        "id": 1,
        "condition": false
    },
    {
        "id": 2,
        "condition": true
    }
]

In this case, I want the output to be "id": 1, since it has condition as both true and false ("id": 2 only has condition as true, so it does not qualify).

Thank you"

英文:

I have these documents:

[
    {
        "id": 1,
        "condition": true
    },
    {
        "id": 1,
        "condition": false
    },
    {
        "id": 2,
        "condition": true
    },
]

In this case I want the output to be "id": 1, since it has condition as both true and false ("id": 2 only has condition as true, so it does not qualify)

Thank you

答案1

得分: 2

以下是翻译好的部分:

一个选项是对于一般情况下的n个中的m个条件,可以使用$setIsSubset

db.collection.aggregate([
  {$group: {
      _id: "$id",
      conditions: {$addToSet: "$condition"}
  }},
  {$match: {$expr: {
        $setIsSubset: [
          [true, false],
          "$conditions"
        ]
      }
  }},
  {$project: {id: 1}}
])

playground示例中查看它的运行方式。

英文:

One option is to use $setIsSubset for the generic case of n out of m wanted conditions:

db.collection.aggregate([
  {$group: {
      _id: "$id",
      conditions: {$addToSet: "$condition"}
  }},
  {$match: {$expr: {
        $setIsSubset: [
          [true, false],
          "$conditions"
        ]
      }
  }},
  {$project: {id: 1}}
])

See how it works on the playground example

答案2

得分: 1

你可以创建一个聚合管道,首先按照 id 进行分组,并获取一个 conditions 数组。然后检查条件数组是否包含 true 和 false,并创建条件存在的字段 cond_truecond_false
如果 cond_truecond_false 都为 true,则匹配。

db.collection.aggregate([
  {
    $group: {
      _id: "$id",
      conditions: { $push: "$condition" }
    }
  },
  {
    $project: {
      _id: 0,
      id: "$_id",
      cond_true: { $in: [ true, "$conditions" ] },
      cond_false: { $in: [ false, "$conditions" ] }
    }
  },
  {
    $match: { cond_true: true, cond_false: true }
  },
  {
    $project: { id: 1 }
  }
])

playground

英文:

you could create an aggregation pipeline in a way first group by id and get an array of conditions. then check if the condition array has true and false and create condition existing fields cond_true,cond_false.
If both the cond_true and cond_false are true it is a match.

db.collection.aggregate([
  {
    $group: {
      _id: "$id",
      conditions: { $push: "$condition" }
    }
  },
  {
    $project: {
      _id: 0,
      id: "$_id",
      cond_true: { $in: [ true, "$conditions" ] },
      cond_false: { $in: [ false, "$conditions" ] }
    }
  },
  {
    $match: { cond_true: true, cond_false: true }
  },
  {
    $project: { id: 1 }
  }
])

playground

huangapple
  • 本文由 发表于 2023年4月11日 13:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982534.html
匿名

发表评论

匿名网友

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

确定