18.5. The Mongo Client Command Line Interface

MongoDB manifests itself through the CLI client mongo, provided the server is running. This is the same as any other RDBMS.

$ mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("53a97e0b-db1e-4181-beaf-34e197bbdd61") }
MongoDB server version: 4.0.4
Server has startup warnings:
...
... and a hell of a lot of gibberish
>

Or, less verbose:

$ mongo --quiet
>

or even, if you have configured authentication, which you should:

$ mongo -u nml -p --quiet
>

In case of a hosted solution re Any OS from the installation instructions, you must use the supplied connection string such as[17]

mongo ds012345.mlab.com:56789/dbname -u dbuser -p dbpassword

As usual, the first thing you should check before entering a new program on your computer is how to get out of it when you're done. Therefore, in either of the above cases, finish the session with quit(), or Ctrl-c.

Here follows a series of mongo actions the data, inspiration is from https://docs.mongodb.com/manual/mongo/.

Example 18.4. Start Mongo Client and See Databases
$ mongo --quiet
show dbs
admin          0.000GB
config         0.000GB
contacts       0.000GB
euro2012       0.000GB
local          0.000GB
nodemongotodo  0.000GB
>

The databases admin, config, and local are there by default after a new installation.


Example 18.5. Start Mongo Client to Work with MongoDB

What databases do you have?

$ mongo --quiet
> show dbs
admin          0.000GB
config         0.000GB
contacts       0.000GB
euro2012       0.000GB
local          0.000GB
nodemongotodo  0.000GB
>

Example 18.6. Create a Database and a Collection

The database is created when you insert the first data into a collection in it.

Subsequently, a collection is created when you insert the first data into it.

$ mongo --quiet
> use school
switched to db school
> db.class.insert({name: "dkpbw19a1"})
WriteResult({ "nInserted" : 1 })
> db.people.insert( { "name": "Niels", "role": "teacher" } )
WriteResult({ "nInserted" : 1 })
> db.people.insert( { "name": "Per", "role": "boss" } )
WriteResult({ "nInserted" : 1 })
> db.people.insert({})
WriteResult({ "nInserted" : 1 })
> db.people.insert({ name: "Jane Doe", role: "student", class: "dkpbw19a1"})
WriteResult({ "nInserted" : 1 })
>
> show collections
class
people
>

Example 18.7. Read All Members from Collection
$ mongo --quiet
> use school
switched to db school
> db.people.find()
{ "_id" : ObjectId("5e2a959bda0f422d52482763") }
{ "_id" : ObjectId("5e2a9601da0f422d52482764"), "name" : "Jane Doe", "role" : "student", "class" : "dkpbw19a1" }
{ "_id" : ObjectId("5e2c6b73abbed572e3ea0e45"), "name" : "Niels", "role" : "teacher" }
{ "_id" : ObjectId("5e2c6b83abbed572e3ea0e46"), "name" : "Per", "role" : "boss" }
> 

or, to read one/some matching one/some value(s)

> use school
 switched to db school
> db.people.find({ _id: ObjectId("5e2a9601da0f422d52482764") })
{ "_id" : ObjectId("5e2a9601da0f422d52482764"), "name" : "Jane Doe", "role" : "student", "class" : "dkpbw19a1" }
>

or, to pretty print. Useful only for logging.

$ mongo --quiet
> use school
 switched to db school
> db.people.find().pretty()
{ "_id" : ObjectId("5e2a959bda0f422d52482763") }
{
    "_id" : ObjectId("5e2a9601da0f422d52482764"),
    "name" : "Jane Doe",
    "role" : "student",
    "class" : "dkpbw19a1"
}
{
    "_id" : ObjectId("5e2c6b73abbed572e3ea0e45"),
    "name" : "Niels",
    "role" : "teacher"
}
{
    "_id" : ObjectId("5e2c6b83abbed572e3ea0e46"),
    "name" : "Per",
    "role" : "boss"
}
>

Example 18.8. Delete a Document from a Collection
$ mongo --quiet
> use school
> db.people.insert({ name: "John Doe", role: "student", class: "dkpbw19a1"})
>
> db.people.remove({ _id: ObjectId("5e2a959bda0f422d52482763") })
>
> db.people.find().pretty()

Example 18.9. Dropping a Whole Collection
$ mongo --quiet
> use nmltest
switched to db nmltest
> db.test11.insert({x: 42})
WriteResult({ "nInserted" : 1 })
> db.test12.insert({x:43})
WriteResult({ "nInserted" : 1 })
> show collections
test11
test12
> db.test12.drop()
true
> show collections
test11
>

Example 18.10. Dropping a Whole Database
$ mongo --quiet
> show dbs
admin          0.000GB
config         0.000GB
contacts       0.000GB
euro2012       0.000GB
local          0.000GB
nmltest        0.000GB
nodemongotodo  0.000GB
school         0.000GB
> use nmltest
switched to db nmltest
> db.dropDatabase()
{ "dropped" : "nmltest", "ok" : 1 }
> show dbs
admin          0.000GB
config         0.000GB
contacts       0.000GB
euro2012       0.000GB
local          0.000GB
nodemongotodo  0.000GB
school         0.000GB
>



[17] Refer to https://docs.mlab.com/connecting/ under the heading 'mongo interactive shell'