我正在扩展在this example提供的解决方案,其中我在运算符$map的"in"中添加多个条件.所有条件都必须为真,所以我使用$AND运算符来组合它们.新条件判断文档中是否存在作为关键字的特定字段.我正在用$exists号接线员判断这个.用于判断此条件的代码片段如下

{
 "isTypePattern": {
    $exists: false
  }
}

其中,(判断是否存在)字段的名称为:isTypePattern. 但是,当嵌入到$map操作符下时,该代码段将独立工作,并引发编译错误:

代码如下所示,下面是playground link:

db.collection.find({
  "$expr": {
    "$anyElementTrue": {
      "$map": {
        "input": "$entities",
        "in": {
          $and: [
            {
              "$regexMatch": {
                "input": "Room1",
                "regex": "$$this.id",
                "options": "i"
              },
              
            },
            {
              "isTypePattern": {
                $exists: false
              }
            }
          ]
        }
      }
    }
  }
})

因此,如果有人可以建议如何在$map运算符中嵌入$Existes,那就太好了

推荐答案

也许是这样的:

db.collection.find({
"$expr": {
"$anyElementTrue": {
  "$map": {
    "input": "$entities",
    "in": {
      $and: [
        {
          "$regexMatch": {
            "input": "Room1",
            "regex": "$$this.id",
            "options": "i"
          },
          
        },
        {
          $eq: [
            {
              $type: "$$this.isTypePattern"
            },
            "missing"
          ]
        }
      ]
    }
   }
  }
 }
})

解释:

$expr允许使用$type和聚合比较运算符,如可用于 case 的$eq、$ne、$lt、$gt,但不支持$EXISTS.

playground

Mongodb相关问答推荐

MongoDB v4.4聚合的$getfield替代方案

Spring数据MongoDB(聚合)

Mongoose 排除数组中包含特定嵌套对象的文档

按数组mongodb中的第一个元素排序

如何在 go mongo-driver 中为 options.FindOne() 设置限制

在 DBeaver 中连接到 Redis 或 MongoDB

使用 homebrew 和 Xcode 8.1.1 安装 Mongodb 失败

Mongoose 架构引用和未定义类型ObjectID

我怎样才能排序空值在 mongodb 中是最后排序的?

使用模拟 MongoDB 服务器进行单元测试

MongoDB .NET 未在 upsert 上生成 _id

Spring Mongodb @DBREF

Mongoose:填充填充字段

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

如何在 Mongoose 中更新数组值

MongoDB Compass 过滤器(查询)

MongoDB - 文件大小巨大且不断增长

从 nodejs 到 mongodb 或 mongoose 的动态数据库连接

MongoDb 聚合 $match 错误:Arguments must be aggregate pipeline operators

如何在 golang 和 mongodb 中通过 id 查找