Getting started with MongoDB

Abhishek Shastri
AltCampus
Published in
2 min readFeb 5, 2019

MongoDB is and open-source document database and leading NoSQL database. A powerful source that allows to reveal data using frontend and backend using API.

Start with installing MongoDB using ubuntu terminal. Once we install it using ubuntu terminal then we can start ubuntu server as:

$ sudo service mongod start

As required one can stop the server on mongo db using command:

$ sudo service mongod stop

Once the mongo server is started then we can query data stored in MongoDB using mongo cli.

$ mongo

Mongo server stores data in form of JSON. Where different data is stored with different names. For storing data we create database name. Once database is created with certain name. We can query with data database name. A terminal command show dbswill show all the database name stored in mongo server. Once we get the database name to use then we use use database_name. Now we can make query over the names of collections using command show collections. Thereafter if we found the collection name then we try finding the documents inside the collections. Each data in collections is in form of JSON and called individually a document. So for finding a particular data in collections we make query with the related data using the basic mongoDB commands.

$ db.<collection name>.find({<finding condition>})

In the above command finding condition has to be in form of Object, which is used to find related data in mongoDB database.

Connecting it to Frontend application

To connect this database to frontend application we can use mongoose. Mongoose is a npm package which we can install using command:

$ npm install mongoose

After this mongoose package is installed and required in the root of the app. We use a function called mongoose.connect() function to connect the MongoDB to frontend. This can relate all the frontend components being rendered from data using mongoDB.

mongoose.connect('mongodb:27017/localhost:/test');

MongoDB stores data using certain Schema which is related to data being saved. Schema is an Object which holds type of data to be stored in database. Using mongoose and Schema we create a model of these database which we can access using mongoose.model.

var userSchema = new mongoose.Schema({
name: String
});

Once we save data in mongoDB then we can access it using mongoose command findOne or findBy and can use this data in frontend to display details.

--

--