输入mongodb没有正确过滤记录

时间:2021-07-16 06:29:41

标签: python mongodb types mongodb-compass

应用过滤器

{'info.version': { $type: "string" }}

上述过滤器的输出

c
python
java
ubuntu

MongoDB 版本: 3.6.3

MongoDB 中的数据(将每个列表元素视为单个文档)

[
  {
    "name": "c",
    "info": {
      "version": "2.0"
    }
  },
  {
    "name": "python",
    "info": {
      "version": [
        "2.0",
        "3.0"
      ]
    }
  },
  {
    "name": "java",
    "info": {
      "version": [
        "11.0"
      ]
    }
  },
  {
    "name": "ubuntu",
    "info": {
      "version":"20"
    }
  }
]

预期结果

c(Record)
ubuntu(record)

我在应用过滤器时是否遗漏了什么?

1 个答案:

答案 0 :(得分:1)

你必须使用这个

await db.aggregate([
        {
            $match: {
                "info.version": { $type: "string",$not: {$type: "array" } }
            },
        }

    ]).toArray()
相关问题