Rory
Rory

Reputation: 1492

Update Mongo from Node

I'm new to Node.

From the Mongo shell I issue this command on the collection "system.user"

db.system.users.find({_id: "test.admin"})

And get back this record

{
  "_id": "test.admin",
  "user": "admin",
  "db": "test",
  "credentials": {
    "SCRAM-SHA-1": {
      "iterationCount": 10000,
      "salt": "hBehS02+DJL+XX92dpuXnw==",
      "storedKey": "6TifIE7lc4TLKkMSYC6yjX/WZ5Y=",
      "serverKey": "gqxsc9a6Z8HvBLpZl8bNxiPsYWA="
    }
  },
  "roles": [{
    "role": "root",
    "db": "admin"
  }]
}

My question is has anyone got an example JavaScript that will show me how to update the roles attribute. So that after the JavaScript/node update

db.system.users.find({_id: "test.admin"})

will then read

{
  "_id": "test.admin",
  "user": "admin",
  "db": "test",
  "credentials": {
    "SCRAM-SHA-1": {
      "iterationCount": 10000,
      "salt": "hBehS02+DJL+XX92dpuXnw==",
      "storedKey": "6TifIE7lc4TLKkMSYC6yjX/WZ5Y=",
      "serverKey": "gqxsc9a6Z8HvBLpZl8bNxiPsYWA="
    }
  },
  "roles": [{
    "role": "dbowner",
    "db": "admin"
  }]
}

Changes the "role" : "root" to "role" : "dbowner"

I'm looking for the most "atomic" way to do it, perhaps some sort of json parse. Not just a simple string replace.

Upvotes: 1

Views: 107

Answers (1)

programmerj
programmerj

Reputation: 1684

You might try something like this

db.system.users.updateOne({_id: "test.admin"}, {"roles.role":"dbowner"}, {upsert:1});

Upvotes: 1

Related Questions