0

How do I change the level value to the number 2 in the users array for a user with uid: 543? (Mongoose)

Sample code:

[
  {
    "id": 1,
    "name": "Check",
    "balance": 1,
    "tokens": 2,
    "energy": 2,
    "ownerid": 623465,
    "users": [
      {
        "cid": 1285931,
        "uid": 0,
        "level": 3
      },
      {
        "cid": 3272234,
        "uid": 631,
        "level": 1
      }
    ]
  },
  {
    "id": 2,
    "name": "Check2",
    "balance": 5,
    "tokens": 12,
    "energy": 51,
    "ownerid": 171412,
    "users": [
      {
        "cid": 1874345,
        "uid": 543,
        "level": 3
      },
      {
        "cid": 2434512,
        "uid": 212,
        "level": 1
      }
    ]
  }
]
0

1 Answer 1

1

You have to use positional operator in this way:

With this query you are telling mongo that found the element where users.uid is 543 and then set the value where the query match (i.e. where users.uid is 543) to 2.

db.collection.update({
  "users.uid": 543
},
{
  "$set": {
    "users.$.level": 2
  }
})

Example here

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.