【问题标题】:deleting content from a particular collection从特定集合中删除内容
【发布时间】:2013-05-01 03:33:09
【问题描述】:

我有一个类似的 JSON 文档

{
"branch": [
    {
        "section": [
            {
                "sub": "edc",
                "time": "one hour",
                "frequency": "3"
            },
            {
                "sub": "bee",
                "time": "two hours",
                "frequency": "4"
            }
        ]
    },
    {
        "section": [
            {
                "sub": "ss",
                "time": "one hour",
                "frequency": "2"
            },
            {
                "sub": "ms",
                "time": "two hours",
                "frequency": "5"
            }
        ]
    }
]

}

现在我想删除

{
  "sub": "edc",
  "time": "one hour",
  "frequency": "3"
}

使用以下集合中的"sub":"edc"

我希望查询在 mongo db 中执行更改

【问题讨论】:

  • branch 是包含section 文档的集合吗?
  • "branch" 不是一个集合。上面的 json 文档在 mongo db 中的集合中说“college”。我想更新可以删除 { "sub": "edc", "time": "one hour", "frequency": "3" } 的数据库

标签: json mongodb


【解决方案1】:

你需要使用$pull,虽然我还没有使用嵌套数组。

http://docs.mongodb.org/manual/reference/operator/pull/

类似:(但您需要对其进行测试)

db.yourcoll.update( { "branch.section.sub": 'edu' }, { $pull: { "branch.section.sub": 'edu' } } )

这是一个类似的问题:

How to remove an element from a doubly-nested array in a MongoDB document

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多