MongoDB如何通过键/索引/值编辑/更新文档对象内的数组?

时间:2017-11-09 08:02:54

标签: mongodb object

让我们说MongoDB中有一个文件包含一个像这样的数组ob对象:

addresses: [
    {
      city: "Tel Aviv",
      street: "Alenby",
      streetNumber: 50
    },
    {
      city: "Jerusalem",
      street: "King David",
      streetNumber: 10
    },
    {
      city: "Beer Sheva",
      street: "King Shlomo",
      streetNumber: 20
    }
  ]

针对单个部落的问题

  1. 我们想要更新此客户的第二个地址,从King David 10到King Shaul 35。 我们怎么能这样做?

  2. 我们希望仅通过索引或键或匹配的键+值删除第二个地址。

  3. 我们希望在第一个到第二个之间推送另一个地址,因此首先保留第一个,注入一个新地址,然后下一个地址全部自动编入索引。

  4. 注意:我问我如何使用MongoDB查询语言,即NoSQL。 如果您也有使用AngularJS的解决方案,欢迎使用它,但请将其放在第一个Mongo查询语言(MQL)解决方案之后。

    谢谢!

1 个答案:

答案 0 :(得分:1)

创建集合以演示

> db.so.find().pretty()
{
    "_id" : ObjectId("5a0413ad008e3cd140e59af7"),
    "addresses" : [
        {
            "city" : "Tel Aviv",
            "street" : "Alenby",
            "streetNumber" : 50
        },
        {
            "city" : "Jerusalem",
            "street" : "King David",
            "streetNumber" : 10
        },
        {
            "city" : "Beer Sheva",
            "street" : "King Shlomo",
            "streetNumber" : 20
        }
    ]
}

更新MongoDB中存储在数组中的文档的单个字段。

> db.so.update({ "addresses.street": "King David" }, { $set: { "addresses.$.street" : "King Shaul" }});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.so.find().pretty()
{
    "_id" : ObjectId("5a0413ad008e3cd140e59af7"),
    "addresses" : [
        {
            "city" : "Tel Aviv",
            "street" : "Alenby",
            "streetNumber" : 50
        },
        {
            "city" : "Jerusalem",
            "street" : "King Shaul",
            "streetNumber" : 10
        },
        {
            "city" : "Beer Sheva",
            "street" : "King Shlomo",
            "streetNumber" : 20
        }
    ]
}

从MongoDB中的数组中删除文档。

> db.so.update({}, { $pull: { "addresses" : { "street": "King Shaul"} }});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.so.find().pretty()
{
    "_id" : ObjectId("5a0413ad008e3cd140e59af7"),
    "addresses" : [
        {
            "city" : "Tel Aviv",
            "street" : "Alenby",
            "streetNumber" : 50
        },
        {
            "city" : "Beer Sheva",
            "street" : "King Shlomo",
            "streetNumber" : 20
        }
    ]
}

将文档添加到MongoDB中数组中的特定索引。

> db.so.update({"_id" : ObjectId("5a0413ad008e3cd140e59af7")}, {$push: {"addresses": {$each: [{"city": "New York", "street": "Jones"}], $position: 1}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.so.find().pretty()
{
    "_id" : ObjectId("5a0413ad008e3cd140e59af7"),
    "addresses" : [
        {
            "city" : "Tel Aviv",
            "street" : "Alenby",
            "streetNumber" : 50
        },
        {
            "city" : "New York",
            "street" : "Jones"
        },
        {
            "city" : "Beer Sheva",
            "street" : "King Shlomo",
            "streetNumber" : 20
        }
    ]
}