Hello!
MongoDB Version 4.4.1
I have script to create and update users.
If i run his, then get error: db.runCommand(...).users is undefined
.
But the next run will be completed successfully.
What do thing?
Full Error:
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1bbf33e1-c73f-41a9-9e85-868bf9c20a05") }
MongoDB server version: 4.4.1
Successfully added user: {
"user" : "hubdbadm",
"roles" : [
{
"role" : "readWrite",
"db" : "MAPStorage"
}
],
"mechanisms" : [
"SCRAM-SHA-1"
],
"passwordDigestor" : "client"
}
uncaught exception: TypeError: db.runCommand(...).users is undefined :
@/opt/map/mongodb-test/users.js:40:9
@(shell eval):1:1
Error: error loading js file: /opt/map/mongodb-test/users.js :
@(shell eval):1:1
exiting with code -4
Script:
if (db.runCommand({ usersInfo: { user: "hubdbadm", db: "MAPStorage" } }).users.length === 1) {
userdb = db.getSiblingDB("MAPStorage");
userdb.updateUser(
"hubdbadm",
{
roles: [
{
role: "readWrite",
db: "MAPStorage"
}
],
pwd: "hubdbadm",
mechanisms: [
"SCRAM-SHA-1"
],
passwordDigestor: "client"
}
);
} else if (db.runCommand({ usersInfo: { user: "hubdbadm", db: "MAPStorage" } }).users.length != 1) {
userdb = db.getSiblingDB("MAPStorage");
userdb.createUser({
user: "hubdbadm",
pwd: "hubdbadm",
roles: [
{
role: "readWrite",
db: "MAPStorage"
}
],
mechanisms: [
"SCRAM-SHA-1"
],
passwordDigestor: "client"
});
userdb.auth("hubdbadm", "hubdbadm");
};
if (db.runCommand({ usersInfo: { user: "user_1", db: "db_u1" } }).users.length === 1) {
userdb = db.getSiblingDB("db_u1");
userdb.updateUser(
"user_1",
{
roles: [
{
role: "readWrite",
db: "db_u1"
},
{
role: "readWrite",
db: "db_u1_1"
},
{
role: "readWrite",
db: "db_u1_2"
}
],
pwd: "password",
mechanisms: [
"SCRAM-SHA-1"
],
passwordDigestor: "client"
}
);
} else if (db.runCommand({ usersInfo: { user: "user_1", db: "db_u1" } }).users.length != 1) {
userdb = db.getSiblingDB("db_u1");
userdb.createUser({
user: "user_1",
pwd: "password",
roles: [
{
role: "readWrite",
db: "db_u1"
},
{
role: "readWrite",
db: "db_u1_1"
},
{
role: "readWrite",
db: "db_u1_2"
}
],
mechanisms: [
"SCRAM-SHA-1"
],
passwordDigestor: "client"
});
userdb.auth("user_1", "password");
};