Create¶
The Shell¶
MongoDB provides a command shell that allows you to interact directly with the databases.
You can invoke this shell by simply typing
mongo
You will see something similar to

To exit from this shell simply type
exit
You may also find useful to try the help command:
help
from inside the mongo shell.
A Database¶
Notice that when you start the shell, it connects by default to a “test” database.
We can instead, provide a name of a new database as an argument to the mongo command shell.
The following command will create a database called “entertainment”
mongo entertainment
A Collection¶
Inside this database we can create a new collection by simply inserting one first document into it.
Remember that the concept of a collection is alike to the concept of a table in a relational database.
The name of the collection is provided following the string “db.”, in this case, to refer to a collection called “movies” we use “db.movies”.
We can insert a document by using the insert command, as in:
db.movies.insert
and add to it, as an argument the content of the document:
db.movies.insert(
{
title: "Forrest Gump",
year: 1994,
director: "Robert Zemeckis",
writers: [ "Winston Groom", "Eric Roth" ],
stars: [ "Tom Hanks", "Robin Wright", "Gary Sinise" ]
}
)
Notice that the argument to the insert command is a JSON document.
This JSON text will be converted to BSON for internal storage and network transmission.

To see the collections that are available in the current database we can use the command
show collections
To see the databases that are available we can use the command
show dbs
to use a particular database, we take advantage of the use command
use entertainment