Insert Problems

These are the commands I used. The issue is I can not properly insert any data. No sure if the issue is with user role or the construct of the insert.

Thanks in advance.

use bios


db.createUser({
    user: "mongoAdmin", 
    pwd: "12345678", 
    roles: ["readWrite", "dbAdmin"]
})

db.bios.insertMany([
   {
       "_id" : 1,
       "name" : {
           "first" : "John",
           "last" : "Backus"
       },
       "birth" : ISODate("1924-12-03T05:00:00Z"),
       "death" : ISODate("2007-03-17T04:00:00Z"),
       "contribs" : [
           "Fortran",
           "ALGOL",
           "Backus-Naur Form",
           "FP"
       ],
       "awards" : [
           {
               "award" : "W.W. McDowell Award",
               "year" : 1967,
               "by" : "IEEE Computer Society"
           },
           {
               "award" : "National Medal of Science",
               "year" : 1975,
               "by" : "National Science Foundation"
           },
           {
               "award" : "Turing Award",
               "year" : 1977,
               "by" : "ACM"
           },
           {
               "award" : "Draper Prize",
               "year" : 1993,
               "by" : "National Academy of Engineering"
           }
       ]
   }

] );

Getting this ERROR

2023-02-17T17:47:37.991-0500 E  QUERY    [js] uncaught exception: WriteCommandError({
	"ok" : 0,
	"errmsg" : "not authorized on bios to execute command { insert: \"bios\", ordered: true, lsid: { id: UUID(\"4daafa5f-1b95-460a-8c4b-a303ea05eb50\") }, $db: \"bios\" }",
	"code" : 13,
	"codeName" : "Unauthorized"
}) :
WriteCommandError({
	"ok" : 0,
	"errmsg" : "not authorized on bios to execute command { insert: \"bios\", ordered: true, lsid: { id: UUID(\"4daafa5f-1b95-460a-8c4b-a303ea05eb50\") }, $db: \"bios\" }",
	"code" : 13,
	"codeName" : "Unauthorized"
})
WriteCommandError@src/mongo/shell/bulk_api.js:417:48
executeBatch@src/mongo/shell/bulk_api.js:915:23
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:326:5
@(shell):1:1

] SOLVED SOLUTION [

The tutorial I was following did not mention I needed to create a user “myUser” associate them with the database for example “bios” with a a password. Log out and then login to create the database and add collections to it.

Step one login using admin user:

mongo -u "mongoAdmin" -p "myPassword" --authenticationDatabase "admin"

Step two create the user for your desdired database:

db.createUser({
    user: "myUser", 
    pwd: "anotherpassword", 
    roles: ["readWrite", "bios"]
})

Step three logout :

quit()

Step four login with the user.

mongo -u "myUser" -p "anotherpassword" --authenticationDatabase "bios"

5: Create database and add data for testing.

use bios
db.bios.insert({ x:1 })
db.bios.find()

Hope this help others.

Environment : Linux d64 5.10.0-21-amd64 #1 SMP Debian 5.10.162-1 (2023-01-21) x86_64 GNU/Linux

MongoDB version : 4.2.23

Thanks

1 Like