【问题标题】:type in mongodb not filtering record properly输入 mongodb 没有正确过滤记录
【发布时间】:2021-07-16 06:29:41
【问题描述】:

应用过滤器

{'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)

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

【问题讨论】:

  • 版本是少数记录中的字符串和少数记录中的数组。将其更改为其中一种格式
  • 我正在比较哪个是字符串或数组,如果是字符串,我需要过滤掉记录。数据应采用不同的格式。

标签: python mongodb types mongodb-compass


【解决方案1】:

你必须使用这个

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

    ]).toArray()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多