MongoDB: quick start – installing and training materials

MongoDB: quick start – installing and training materials

Some time ago I wrote about SQL to mention the different databases (SQL record: SQL in 60 minutes), however, were completely omitted on non-relational databases, NoSQL (non-SQL). Today I have for you some fundamentals of MongoDB, the most popular NoSQL databases.

MongoDB is a document database designed for ease of development and scaling

So presents us with MongoDB official documentation already in the introduction and in fact it is in a nutshell contains information that MongoDB.

Plan for today

What is MongoDB?

MongoDB is nierelacyjną database, based on documentsspread thanks to its simple structure based on the JSON format and the possibility of easy scaling horizontally, especially for large data sets. MongoDB is written in C++.

One line in the MongoDB database has the document structure consisting of pairs of “key – value”. MongoDB documents are similar to JSON objects. Values may include other documents, arrays, and arrays of documents.

Install MongoDB

Before we go any further, we should, in General, to install MongoDB on your computer

Installing MongoDB In Windows

1. Download the installer

To install MongoDB on our computer wchodzimi on the page www.mongodb.com/try/download/, select the installation version Community Server and take a pack suitable for our system (right, the parcel will be selected automatically – Windows 10, msi).
MongoDB installation of Windows

In my case the latest version is 4.4.2, but, of course, in that moment, as you read this message, may be available with higher number.

2. Install MongoDB step by step

Further leads us to the installer. Along the way we have to select a Complete or Custom (this option allows you to choose which parts of the software that you can install and custom install path Mongo). I remain at the default setting, Complete.

We also reserve the selected option MongoDB as a Windows service.

Install MongoDB on Windows as a service

Next, you can unselect Install MongoDB Compass, which is a graphical provide an additional interface for MongoDB which is supplied by the editor and a visual tool for managing and analyzing data. If necessary, we can install it.

Go forth, and we give the installation.

3. Start MongoDB

MongoDB is installed in the default folder (i.e. Program Files.
In my case it is: Drive C: > Program Files > MongoDB.

We are looking for file mongo.exe it is located in the installation directory:

Dysk lokalny > Program Files > MongoDB > Server > 4.x > bin

Run MongoDB On Windows 10

After clicking on the file mongo.exe must run the command prompt window, sheath with information about how to connect to MongoDB:

console MongoDB

At this point, you can use the MongoDB console, for example, to run the command db.help() useful commands Mongo.

4. Add MongoDB for environment variables

However, we can’t use Mongo from anywhere on the computer. If I now windows command prompt (cmd) and type mongo, I get the information that this command does not exist.
Only kliknięce in the file mongo.exe in the corresponding directory you run the MongoDB client. It is not too convenient, so we want (as in Python and many plants) to add mongo in system variables (add to PATH’while 😉 ).

Search and Modify system environment variables (Edit system environment variables). In section Advanced at the bottom click Environment variables…

Add MongoDB for environment variables

In this place we choose a variable Path and select Change…

Mongo DB Windows - adding to PATH

We will open a new window. Select New and add the path to the folder bin for MongoDB. or to put it in, or choose from your side Browse… and wyklikać right place.

I have:

C:Program FilesMongoDBServer4.4bin

MongoDB add environment variable

Don’t forget to approve all OK.

Now when you open a new command prompt window and entering mongo we start the MongoDB shell. We can close it and go to the “normal” mode the console using Ctrl + C
MongoDB console

Installing MongoDB On MacOS

Install according to instructions in the documentation. We use homegrown tools (if you don’t have homegrown start with a box).

Open the Terminal app or iTerm.

Install MongoDB Community Edition using the brew in the 2 commands:

brew tap mongodb/brew

then:

brew install mongodb-community

MongoDB has been installed on Your computer.

Start mongodb
brew services run mongodb-community

or

brew services start mongodb-community

MongoDB runs as a service in the background. Here’s what you will see:

$ brew services start mongodb/brew/mongodb-community
==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

Re-run this command returns information that the Mongo instance is already running, but we can always check the list of running services, use the command:

brew services list

When mongodb community you should see the status started.

What is the difference between run and start how ?

Team start starts a service, in this case, MongoDB, along with the input of the computer, while run this is a one-off run. For the completion of this service, you must run the command

brew services stop mongodb-community

Install MongoDB On Linux

Depending on Linux distributions, in the documentation there is a separate manual. The differences are small, but still worth a go through the official installation https://docs.mongodb.com/manual/installation/#mongodb-community-edition-installation-tutorials or view on YOUTUBE 😉

Use the Mongo

If MongoDB is running, we have access to the Mongo console. Just enter the command:

mongo

console MongoDB macOS

To exit the Mongo console with Ctrl + C.

Verify that MongoDB is running properly

After the launch of the console in the terminal shows some initial information. Team mongo makes connecting to a locally running instance of MongoDB version of the shell, the address of the running instance localhost (127.0.0.1) and port (the default port to run this 27017), session id and the server version.

If later in the process, you want to quickly check the version of the MongoDB client running. Simply run the command db.version().

Teams, of course, much, much more.

Basic commands

Będząc in mongo console, we check the list of available commands MongoDB client. Type db.help(). The result is a long list of available instructions that are understood to MongoDB:
MongoDB basic commands

We check what databases we currently use a simple command db. The result is the following:

> db
test

Our primary base is the database name test.

We can also check the stats of the MongoDB server. Enter the command db.stats()to see the name of the database, the number of collections and documents in our database.

At the start, but not the only base is base testand according to statistics, almost all values are set to 0.

To view the list of available databases, you must run the command show dbs.

Adding test data

Yeah, I said that in the database we have a collection. The collections include the documents, but you do not need to be imposed on the structure, even if you don’t specify, it’s such a struktry no, we therefore base to throw any data. For example,

db.test.save( { hello: "world" } )

Saved a couple of “key-value”.

So focus on the contents of the collection team db.collectionName.find():

db.test.find()

As a result, we will see “_id” indicating ObjectId together with the contents of our steam “key-value”.

Super our database is MongoDB installed correctly. You can move on with science.

Materials for the study

A few of my suggestions, materials for learning from scratch. Materials, free or paid, but cheap 😉

➔ Free rate MongoDB Data Wrangling from Udacity (courses provided free of charge under the way Data Analyst Nanodegree)

➔ Course MongoDB 101 from Academy 3T for free at the basic level. In part the front is not all available for free.

➔ Video MongoDB course on Udemy The Complete Developer Guidethe only paid from the list, but a comprehensive and cheaply (approx. 10$). If you are interested in Mongo + Node.js also available on Udemy course Node.js, Express, MongoDB &More.

Ready to see us in action:

More To Explore

IWanta.tech
Logo
Enable registration in settings - general
Have any project in mind?

Contact us:

small_c_popup.png