MongoDB is a document database meaning that its content is not tables with data, but rather collections of documents with content. The documents are json documents. We say that
A document is a json document:
{
"name": "Niels",
"email": "nmla@iba.dk",
"role": "teacher"
}
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[10]
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/.
$ 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.
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 >
The database is created when you insert the first data into a collection in it.
Subsequently, a collection is created when you first insert 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
>
$ 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"
}
>
$ mongo --quiet
> use school
> db.people.insert({ name: "John Doe", role: "student", class: "dkpbw19a1"})
>
> db.people.remove({ _id: ObjectId("5e2a959bda0f422d52482763") })
>
> db.people.find().pretty()
$ 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
>
$ 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
>
Backup is done per database with the following where
world is the database.
mongodump -d world
resulting in
dump
└── world
├── city.bson
└── city.metadata.json
This structure may be zipped and transported between users.
If necessary, and if appropriate, it may be handled by
git in some project.
Restoring a database is done by
mongorestore -d world --drop dump/world
The two prorams mongodump, and
mongorestore might have to be installed separately.
The may be in a mongo-utilities package.