1

Lets say we have a document in MongoDB contains an array ob objects like this:

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

THE QUESTION SLICED FOR SINGLE UNDER PIECES

  1. We want to update the second address of this client, from King David 10 to King Shaul 35. How can we do that?

  2. We want to remove only the second address by index or key or matched key+value.

  3. We want to push another address between first to second, so first will remain first, a new address will be injected, and all of the next will be auto indexed by next.

Note: I am asking how do I do that using MongoDB query language, aka NoSQL. If you also have a solution for doing it using AngularJS it will be welcome, but please place it after the first solution for Mongo query language (MQL).

Thanks!

3
  • Using what? The question is tagged with MongoDB and AngularJS. So do you want to change an array in your client code? Or do you want to know how to update in your database? Pick One. Because if you're asking for an end to end tutorial your question is way too broad. So is the general concept of "questions" as opposed to one question, which is the actual format here. Pick one question and decide if it's a front or back end question. Commented Nov 9, 2017 at 8:12
  • I am asking how to do that with NoSQL of MongoDB, like db.users.update Commented Nov 9, 2017 at 8:13
  • Please edit your question to make it more precise. Thank you. Commented Nov 9, 2017 at 8:20

1 Answer 1

1

Creating collection in order to demonstrate

> 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
        }
    ]
}

Update a single field of a document stored in an array in 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
        }
    ]
}

Remove a document from an array in 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
        }
    ]
}

Add a document to a specific index in an array in 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
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.