0

I want to update this document...

{
    "_id" : "2QCTWXJHMa3Sjnta5",
    "text" : [
        {
            "id" : "krD8yXFEEd8Esc4Qc",
            "sentence" : "dasdsada."
        },
        {
            "id" : "AwXHZmPKSPyKvd9E9",
            "sentence" : "adsdadas ."
        },
        {
            "id" : "bsf6RyKDQyrnqsmK7",
            "sentence" : "daadadadad"
        }
    ]
}

... to this

{
    "_id" : "2QCTWXJHMa3Sjnta5",
    "text" : [
        {
            "id" : "krD8yXFEEd8Esc4Qc",
            "sentence" : "dasdsada."
        },
        {
            "id" : "AwXHZmPKSPyKvd9E9",
            "sentence" : "Just changed"
        },
        {
            "id" : "bsf6RyKDQyrnqsmK7",
            "sentence" : "daadadadad"
        }
    ]
}

Therefore I tried:

Collection.update(
    { _id: '2QCTWXJHMa3Sjnta5', text.id: 'AwXHZmPKSPyKvd9E9' },
    { $set: { sentence: "Just changed" }}

);

What am I doing wrong?

1 Answer 1

1

You need to identify an element in an array in order to update it. You can do it with the positional $ operator.

Be aware that positional $ operator acts as a placeholder for the first element in array that matches the query. So you can update only the first matched element in array with it.

Try the following query:

Collection.update(
  { _id: '2QCTWXJHMa3Sjnta5', 'text.id': 'AwXHZmPKSPyKvd9E9' },
  { $set: { 'text.$.sentence': "Just changed" }}

);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.