1

I have schema like :

const UserSchema = new mongoose.Schema({
    skills: [{
        _id: false,
        name: String,
        level: Number,
        exp: Number
    }]
});

As example document is like :

{
    "_id": {
        "$oid": "5b0941419703f80a121c44df"
    },
    "skills": [
        {
            "name": "woodcutting",
            "exp": 1110,
            "level": 1
        },
        {
            "name": "smithing",
            "level": 0,
            "exp": 0
        },
        {
            "name": "reading",
            "level": 0,
            "exp": 0
        },
        {
            "name": "writing",
            "level": 0,
            "exp": 0
        }
    ]
}

So I just need to update exp and level of woodcutting and smithing in one query, is it possible, if possible, how? Or will I need to change all array and set(replace) with it value of skills?

1 Answer 1

1

You can use arrayFilters from mongodb 3.6

db.collection.update(
  { }, 
  { "$set": {
    "skills.$[skill1].exp": "your_value",
    "skills.$[skill2].level": "your_value",
    "skills.$[skill2].exp": "your_value",
    "skills.$[skill1].level": "your_value"
  }},
  { "arrayFilters": [{ "skill1.name": "woodcutting", "skill2.name": "smithing" }] }
)
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.