March 2019
Intermediate to advanced
394 pages
11h 4m
English
When writing scripts for the mongo shell, we cannot use the shell helpers. MongoDB's commands, such as use <database_name>, show collections, and other helpers are built into the shell and so are not available from the JavaScript context where our scripts will get executed. Fortunately, there are equivalents to them that are available from the JavaScript execution context, as shown in the following table:
|
Shell helpers |
JavaScript equivalents |
|
show dbs, show databases |
db.adminCommand('listDatabases') |
|
use <database_name> |
db = db.getSiblingDB('<database_name>') |
|
show collections |
db.getCollectionNames() |
|
show users |
db.getUsers() |
|
show roles ... |
Read now
Unlock full access