Home » MongoDB

How to insert a document in MongoDB?

In this article, we are going to insert a document (dummy JSON data) inside a collection of a database.
Submitted by Manu Jemini, on February 02, 2018

MongoDB is a non-sql database. It is used to store data without the usage of SQL queries. MongoDB is a document based data-based made for high performance, high availability, and automatic scaling.

This make it easy for web developers to manage data-base because of the simplicity in which the data is been store in MongoDB.

A record is a mongo document that is composed of field and value pairs. The documents are like JSON objects. Just like JSON in mongo we can put more documents inside a document.

To insert a document in MongoDB through an Express server we need to full fill certain requirements:

  1. You must have MongoDB install: npm install MongoDB
  2. You must import, mogodb in the express server: var MongoDB = require('MongoDB')
  3. You should always specify the URL for MongoDB in accordance with your system: var url = "MongoDB://localhost:27017/vehicle";

After fulfill the above three conditions, we should make connection to the database like this: MongoClient.connect(url, function(err,db){})

Then we have to make sure if there is any error or not with an if-else statement. If there is no error open a collection with its name: var collection = db.collection('cars');

In the example below we make a dummy data to fill in the data-base. To understand how it happens here is an example:

var document = [
	{	
		Name: "Abhishek Shamra"
	}
];

Then simply insert this JSON inside database by using an easy function: collection.insert(document,function(err,result){ })

Just like before we have to check if there is any error or not by using an if-else statement.

In the end just close the database by:

db.close();

JS file:

// require MongoDB
var MongoDB = require('MongoDB');
var MongoClient = MongoDB.MongoClient;
//create url
var url = "MongoDB://localhost:27017/vehicle";
//connect with mongo client
MongoClient.connect(url, function(err,db){
	if(err)
	{
		console.log(err);
	}
	else
	{
		console.log('Connected to ',url);

		var collection = db.collection('cars');
		//JSON document
		var document = [
		{
			"id": "0001",
			"type": "sedan",
			"company_name": "nissan",
			"qty": 200,
			"model_info":
			{
				"model_name":
				[
					{ "id": "1001", "name": "nissan sunny" },
					{ "id": "1002", "name": "nissan sylphy" },
					{ "id": "1003", "name": "nissan teana" }
				],
				"type":
				[
					{ "id": "5001", "name": "Petrol" },
					{ "id": "5002", "name": "Diesel" },
				]
			}
		},
		{
			"id": "0002",
			"type": "sports",
			"company_name": "ferrari",
			"qty": 100,
			"model_info":
			{
				"model_name":
				[
					{ "id": "1001", "name": "448 gtb" },
					{ "id": "1002", "name": "488 spider" },
					{ "id": "1003", "name": "ferrari portofino" }
				],
				"type":
				[
					{ "id": "5001", "name": "Petrol" },
					{ "id": "5002", "name": "Diesel" },
				]
			}
		},
		{
			"id": "0003",
			"type": "suv",
			"company_name": "honda",
			"qty": 78,
			"model_info":
			{
				"model_name":
				[
					{ "id": "1001", "name": "wr-v" },
					{ "id": "1002", "name": "br-v" }
				],
				"type":
				[
					{ "id": "5001", "name": "Petrol" },
					{ "id": "5002", "name": "Diesel" },
				]
			}
		}
		];

		collection.insert(document,function(err,result){
		if(err)
		{
			console.log(err);
		}
		else
		{
			console.log('%d document inserted',result.insertedCount);
		}
		db.close();
		});
	}
});

Server running

MongoDB - Insert a document

Output on console

MongoDB - Insert a document



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.