【问题标题】:How do I properly use deleteMany() with an $and query in the MongoDB shell?如何在 MongoDB shell 中正确使用 deleteMany() 和 $and 查询?
【发布时间】:2021-09-28 16:52:11
【问题描述】:

我正在尝试删除我的集合 infrastructure 中具有“管道”type.primary 属性和“石油”type.secondary 属性的所有文档。

我正在尝试使用以下查询: db.infrastructure.deleteMany({$and: [{"properties.type.primary": "pipelines"}, {"properties.type.secondary": "oil"}] }),

返回:{ acknowledged: true, deletedCount: 0 }

我希望我的查询能够正常工作,因为在 MongoDB Compass 中,我可以检索与查询 {$and: [{"properties.type.primary": "pipelines"}, {"properties.type.secondary": "oil"}] } 匹配的 182 个文档

我的文档显示为以下结构(仅相关部分):

properties": {
                "optional": {
                    "description": ""
                },
                "original": {
                    "Opername": "ENBRIDGE",
                    "Pipename": "Lakehead",
                    "Shape_Leng": 604328.294581,
                    "Source": "EIA"
                },
                "required": {
                    "unit": null,
                    "viz_dim": null,
                    "years": []
                },
                "type": {
                    "primary": "pipelines",
                    "secondary": "oil"
                }
...

我的理解是我只需要将过滤器传递给deleteMany() 并且$and 需要一个对象数组。由于某种原因,两者结合在这里不起作用。

【问题讨论】:

  • 你在哪里执行这个删除查询?
  • @turivishal 在 MongoDB Compass mongo shell 中
  • 在执行查询之前你是否执行了命令use <database name>
  • @turivishal 是的
  • @turivishal 进一步审查后,我意识到我的数据库名称拼写错误。感谢您的帮助。

标签: json database mongodb mongodb-query bson


【解决方案1】:

我意识到最简单的答案是正确的——我拼错了我的数据库名称。

【讨论】:

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