Skip to content

mongosh

Info

MongoDB can be manipulated with the mongo command,
However, if you use the "mongo" command, you will receive a warning that the "mongo" command is deprecated and will be removed in a future release.
This is because the "mongo" command will be replaced by the more user-friendly and compatible "mongosh" command.
For this reason, the following is a description of how to use mongosh.

mongosStarting mongosh

mogosh

1
mongosh
1
2
3
4
5
6
7
8
Current Mongosh Log ID: [id]
Connecting to:  mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.1
Using MongoDB:  7.0.2
Using Mongosh:  2.0.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test>

Warning

For security reasons, we recommend avoiding command lines containing passwords. Instead, authentication can be performed after connecting in the MongoDB shell.

1
2
3
4
mongosh
> use admin
switched to db admin
> db.auth("admin","[password]")

Info

The following warning may appear in mongosh.

1
2
Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded.
You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.

In that case, just rename it as described.

1
2
cp ~/.mongorc.js ~/.mongorc_old.js
mv ~/.mongorc.js ~/.mongoshrc.js

Info

If admin is specified

1
2
3
4
5
6
7
mongosh -u admin -p --authenticationDatabase admin

Enter password: **********
Current Mongosh Log ID: 652a46b96004ccbf87fa73d4
Connecting to:  mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.0.1
Using MongoDB:  7.0.2
Using Mongosh:  2.0.1

For registered user designation

1
2
3
4
5
6
7
8
9
mongosh -u [user] -p --authenticationDatabase [db]

Enter password: ******
Current Mongosh Log ID: [id]
Connecting to:  mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=[db名]&appName=mongosh+2.0.1
Using MongoDB:  7.0.2
Using Mongosh:  2.0.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Check the version of Mongosh

db.version()

1
2
test> db.version()
6.0.4

Listing of databases

show dbs

1
2
3
4
5
test> show dbs
admin       180.00 KiB
config      108.00 KiB
ec_sol_db1   92.00 KiB
local        76.00 KiB

Info

When you first log in, you may see the following error message

1
2
test> show dbs
MongoServerError: Command listDatabases requires authentication

When an error is displayed, you must log in again with authentication.

1
mongosh -u admin -p --authenticationDatabase admin

Select the DB

use [db]

1
2
test> use ec_sol_db1
switched to db ec_sol_db1

Check the collection

show collections

1
2
ec_sol_db1> show collections
ec_sol_pes_tbl

Check access privileges in the user list

admin> db.system.users.find()

If you want to check access privileges in the user list, you must log in with the admin designation.

1
mongosh -u admin -p --authenticationDatabase
1
2
test> use admin
switched to db admin
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
admin> db.system.users.find();
[
  {
    _id: 'admin.admin',
    userId: new UUID("[UUID]"),
    user: 'admin',
    db: 'admin',
    credentials: {
      'SCRAM-SHA-1': {
        iterationCount: 10000,
        salt: '[salt]',
        storedKey: '[storedKey]',
        serverKey: '[serverKey]'
      },
      'SCRAM-SHA-256': {
        iterationCount: 15000,
        salt: '[salt]',
        storedKey: '[storedKey]',
        serverKey: '[serverKey]'
      }
    },
    roles: [
      { role: 'userAdminAnyDatabase', db: 'admin' },
      { role: 'readWrite', db: '[db]' }
    ]
  },
  {
    _id: '[db].[user]',
    userId: new UUID("[UUID]"),
    user: '[user]',
    db: '[db名]',
    credentials: {
      'SCRAM-SHA-1': {
        iterationCount: 10000,
        salt: '[salt]',
        storedKey: '[storedKey]',
        serverKey: '[serverKey]'
      },
      'SCRAM-SHA-256': {
        iterationCount: 15000,
        salt: '[salt]',
        storedKey: '[storedKey]',
        serverKey: '[serverKey]'
      }
    },
    roles: [ { role: 'readWrite', db: '[db]' } ]
  }
]

admin registration

The user administrator (admin) must be registered to use the DB for the first time.

db.createUser()

1
2
3
mongosh
use admin
db.createUser({user: "admin", pwd: "[password]", roles: ["userAdminAnyDatabase"]})

Database Creation

use [db]

Create a database (database name is "myDatabase")

1
use myDatabase

Collection Creation

db.createCollection('コレクション名')

Create a collection (name the collection "myCollection")

1
db.createCollection('myCollection')

Registration of a specific user

db.createUser()

Connect to a specific database (database name is "myDatabase")

1
2
use myDatabase
db.createUser({user: "user", pwd: "[password]", roles: ["readWrite"]})

db.grantRolesToUser()

If you wish to grant the same privileges to other databases, do the following
Connect to a specific database (database name is "anotherDatabase")

1
2
use anotherDatabase
db.grantRolesToUser("user", [{role: "readWrite", db: "anotherDatabase"}])

It is a good idea to add root privileges to admin privileges.
Please be careful with password management and permission settings as you have access to all db's and collections.

1
db.grantRolesToUser("admin", ["root"])

Check the contents of the collection

db.ec_sol_pes_tbl.find()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[test> use ec_sol_db1
switched to db ec_sol_db1
ec_sol_db1> db.ec_sol_pes_tbl.find()
[
  {
    _id: ObjectId("65294a82e062ec915f2c95be"),
    col1: ISODate("2023-03-03T00:05:14.071Z"),
    col2: 'ALONE',
    col3: 414,
    col4: 1,
    col5: 87.99,
# …

Comments