Mongo 聚合 JS 'every' 等效

Mongo Aggregate JS 'every' equivalent

我正在使用 aggregate,我想为每个 Document 添加一个 Boolean property,如果 subArray 中的所有属性在 Document 上是正确的。

在我下面的示例中...CONFLICTS 是文档的一个子数组,每个文档都包含一个名为 ResolvedBoolean property。如果所有冲突都已解决,我只想添加一个新的 allResolved 字段。

所以..在我的addFields阶段,我有类似

的东西
 {
   '$project': {
      'allResolved': {
        '$allElementsTrue': '$CONFLICTS.resolved'
      }, 
    }
  }

所以 JS 等价于 const allResolved = CONFLICTS.every(conflict => conflict.resolved)

但这似乎总是 return true 这是不正确的

查询

  • 这会进行额外检查
  • 检查为数组
  • 非空数组
  • 并且每个成员都为真(缺失和空值将为假)

Test code here

aggregate(
[{"$set":
  {"allResolved":
   {"$and":
    [{"$isArray":["$conflicts"]},
     {"$not":[{"$eq":["$conflicts", []]}]},
     {"$reduce":
      {"input":"$conflicts",
       "initialValue":true,
       "in":
       {"$and":[{"$eq":["$$this.Resolved", true]}, "$$value"]}}}]}}}])