+3 votes
in Web development by (242k points)
MongoDB tutorial: introduction and installation

1 Answer

+4 votes
by (1.6m points)
Best answer

Step 1: installation
Step 2: start the MongoDB server
Step 3: start the client
Step 4: create a database
Step 5: create collection
Step 6: add documents
Step 7: manage documents


MongoDB tutorial: introduction and installation

In general, the different database models, SQL or NoSQL, differ significantly in terms of their configuration and operation. The open source software MongoDB is the perfect example to show that even when using a NoSQL database, this does not necessarily mean that all knowledge about relational databases has to be thrown away. Although the document-oriented database differs greatly from classics like MySQL, both models share certain similarities that you will find in our guide on MongoDB. Although MongoDB requires familiarity with the query language and command syntax, getting started with it is not very difficult for experienced SQL users. 

In the following MongoDB tutorial we deal with the installation, configuration and management of this modern database system, using Ubuntu as an example ..

Step 1: installation

In the download area of ​​the official MongoDB page you will find the open source edition? Community Server ?, as well as the commercial solution for companies. The first step is to select the installation binaries for your system and download them . MongoDB is a cross-platform program, so it adapts to a wide variety of systems, such as Windows, Linux, OS X, and Solaris.

On a Windows operating system, it is possible to install the database by simply using the downloaded installation file and storing it in the appropriate directory. Windows 10 users can use the version for Windows Server 2008 (64-bit). For Linux and other UNIX systems it is necessary to download the file, unzip it and install it with the help of the corresponding package manager. Depending on the distribution, you will have to import the GPG public key for MongoDB. Ubuntu requires this authentication key, so the following command must be implemented:

  sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927  

Then update the package manager list:

  sudo apt-get update  

Finally, install MongoDB, as well as other useful management tools:

  sudo apt-get install -y mongodb-org  

Step 2: start the MongoDB server

If you wish, in the configuration file /etc/mongod.conf you can change the installation directory assigned by default / var / lib / mongodb, as well as the log directory / var / log / mongodb. Use the following command to start the database:

  sudo service mongod start  

If, instead of using the start parameter , the stop parameter is used , the database execution is terminated ; restart will take care of restarting it. To check if MongoDB has started successfully, you can find the following command line in the /log/mongodb/mongod.log log file :

  [initandlisten] waiting for connections on port <port>  

This indicates that the database server is running and is waiting for incoming connections from the port defined (<port>) in the configuration file. Port 27017 is used by default..

Step 3: start the client

After the MongoDB server is running, the client can be started. At this point, the Mongo shell is used, the command interface included by default, based on JavaScript and used for the administration of the database , as well as for accessing and updating the data set . With this command you can start the client within the same system where the MongoDB application is running:


Mongo shell automatically connects to the Mongo DB instance running on the host and local port 27017 . It is also possible to modify the standard settings and adapt them to your own needs. The following table summarizes these and other important options:




Activates the shell interface and presents it after executing a command.


Prevents Mongo shell from connecting to a database..

--port <port>

Defines the port for the connection.

--host <hostname>

Defines the host for the connection.

--help or -h

Show options.

- username <username> or -u <username>

Once the access permissions have been defined, you will be able to access with your respective username ( <username> ).

--password <password> or -p <password>

When the access permissions have been defined, you will be able to access using the corresponding password (<password>). </password>

The less than (<) and greater than (>) symbols used in the table above are not part of the parameter as such and therefore do not appear in the final command. The process of defining the port means, for example, that if you decide to use port 40000 instead of 27017, it will look like this:

  mongo --port 40000  

Step 4: create a database

Once MongoDB and the client are up and running, you can focus on managing and editing data. However, first of all you need to create a database, otherwise the dataset (collections) and documents will be stored in the auto-generated test database. E l command use facilitates the creation of a database . If you want to create one with the name mydatabase , use the following command:

  use mibasededatos  

The use command also allows you to select an existing MongoDB database in case you want to use it for data processing . With the help of the short db command , you can identify which database is currently selected.

Step 5: create collection

The next step is to create your first collection or file folder for the different BSON documents , where, later, all the data will be saved. The basic syntax is this:

  db.createCollection(<name>, { options } )  

Thus, the command has both parameters: name (name of the file folder) and options (configuration options for collection). The options specify, for example, if the size of the files should be limited ( capped: true ) or if the number of bytes ( size: <number> ) or the number of documents ( max: <number> should be limited ) in a collection. A collection named myfiles folder , with a byte limit of 6,142,800 and a maximum of 10,000 documents would be created, for example, with the following command (the white space only serves to provide better clarity:

  db.createCollection ("micarpetadearchivos", { capped: true, size: 6142800, max: 10000 } )  

Step 6: add documents

Once the folder has been created, documents can be added to it. There are mainly three methods for this:

  • .insertOne ()

  • .insertMany ()

  • .insert ()

In this way it is possible to add just one document ( .insertOne ), some documents ( .insertMany ) or a large volume of documents ( .insert ). The following example shows a database entry with three simple pieces of information: name, age and occupation, which will be inserted into the folder created in the previous step.

  db.micarpetadearchivos.insertOne( { Nombre: "nombre", Edad: 28, Ocupación: "estudiante" } )  

Thus, both for this entry and for all future entries, MongoDB automatically generates a unique identifier for each collection.

Step 7: manage documents

In the final stage of our MongoDB tutorial we show the basic management process of the created documents. In order to make any change to a document, you must find it. This request is accomplished with the find command and can, if necessary, be rendered and filtered with the query filter ( query filters) and projection (specification of the results) parameters . For example, to access the document generated in the previous step, the following command is used:

  db.micarpetadearchivos.find( { Nombre: "nombre", Edad: 28 } )  

If you want to update this document, you can use the update function . To do this, choose the value you want to change, select an update operator and specify the new value. For example, to change the age of the previous case, you need the $ set operator :

  db.micarpetadearchivos.update( { Edad: 28 }, { $set: { Edad: 30 } } )  

To learn about other update operators , visit the English documentation on the official MongoDB website.

To remove the documents from a collection, use the remove command :

  db.micarpetadearchivos.remove ()  

The only way to delete individual documents or collections is by defining criteria such as file identification or other exact values ​​that allow MongoDB to determine what type of record it is. The more specific you are, the more exact the system will act when deleting files. For example, the command:

  db.micarpetadearchivos.remove ( { Edad: 28 } )  

removes all entries with the value 28 for the "Age" field. Using the justOne (1) parameter it is also possible to specify that you only want to remove one of the entries with that value:

  db.micarpetadearchivos.remove ( { Edad: 28 }, 1 )  

For more information on, for example, managing users, security settings, creating replicas, or distributing data across multiple systems, visit the official documentation at mongodb.com or the MongoDB tutorial at tutorialspoint.com.