Inserting Many Rows in MongoDB

MongoDB has an absolutely fine syntax. Inserting a record and inserting many record syntax isn’t doesn’t have much of a difference.

This is how you insert a row in MongoDB

> db.books.insert({
  title: 'Book Title',
  category: 'Fiction',
  tags: ['children', 'fantasy'],
  user: {
    name: 'Sophia',
    status: 'author'
  },
  date: Date()
})
WriteResult({ "nInserted" : 1 })

You should use the insertMany method on the collection to insert multiple rows in MongoDB.

> db.books.insertMany([{
  title: 'Book Title 2',
  category: 'Fiction',
  tags: ['teen', 'fantasy'],
  user: {
    name: 'Ryan',
    status: 'author'
  },
  date: Date()
},
{
  title: 'Book Title 3',
  category: 'Non-Fiction',
  tags: ['history', 'documentary'],
  user: {
    name: 'Arthur',
    status: 'author'
  },
  date: Date()
},
{
  title: 'Book Title 4',
  category: 'Non-Fiction',
  tags: ['legal'],
  user: {
    name: 'Manuel',
    status: 'author'
  },
  date: Date()
}])

{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("5d5e21a47545de885fa391fc"),
		ObjectId("5d5e21a47545de885fa391fd"),
		ObjectId("5d5e21a47545de885fa391fe")
	]

Showing the rows confirms that our action succeeded

> db.books.find()
{ "_id" : ObjectId("5d5e1c357545de885fa391fb"), "title" : "Book Title", "category" : "Fiction", "tags" : [ "children", "fantasy" ], "user" : { "name" : "Sophia", "status" : "author" }, "date" : "Thu Aug 22 2019 12:38:12 GMT+0800 (PST)" }
{ "_id" : ObjectId("5d5e21467545de885fa391fc"), "title" : "Book Title 2", "category" : "Fiction", "tags" : [ "teen", "fantasy" ], "user" : { "name" : "Ryan", "status" : "author" }, "date" : "Thu Aug 22 2019 12:59:50 GMT+0800 (PST)" }
{ "_id" : ObjectId("5d5e21a47545de885fa391fd"), "title" : "Book Title 3", "category" : "Non-Fiction", "tags" : [ "history", "documentary" ], "user" : { "name" : "Arthur", "status" : "author" }, "date" : "Thu Aug 22 2019 13:01:24 GMT+0800 (PST)" }
{ "_id" : ObjectId("5d5e21a47545de885fa391fe"), "title" : "Book Title 4", "category" : "Non-Fiction", "tags" : [ "legal" ], "user" : { "name" : "Manuel", "status" : "author" }, "date" : "Thu Aug 22 2019 13:01:24 GMT+0800 (PST)" }

To make things prettier, we can use the pretty method

> db.books.find().pretty()
{
	"_id" : ObjectId("5d5e1c357545de885fa391fb"),
	"title" : "Book Title",
	"category" : "Fiction",
	"tags" : [
		"children",
		"fantasy"
	],
	"user" : {
		"name" : "Sophia",
		"status" : "author"
	},
	"date" : "Thu Aug 22 2019 12:38:12 GMT+0800 (PST)"
}
{
	"_id" : ObjectId("5d5e21467545de885fa391fc"),
	"title" : "Book Title 2",
	"category" : "Fiction",
	"tags" : [
		"teen",
		"fantasy"
	],
	"user" : {
		"name" : "Ryan",
		"status" : "author"
	},
	"date" : "Thu Aug 22 2019 12:59:50 GMT+0800 (PST)"
}
{
	"_id" : ObjectId("5d5e21a47545de885fa391fd"),
	"title" : "Book Title 3",
	"category" : "Non-Fiction",
	"tags" : [
		"history",
		"documentary"
	],
	"user" : {
		"name" : "Arthur",
		"status" : "author"
	},
	"date" : "Thu Aug 22 2019 13:01:24 GMT+0800 (PST)"
}
{
	"_id" : ObjectId("5d5e21a47545de885fa391fe"),
	"title" : "Book Title 4",
	"category" : "Non-Fiction",
	"tags" : [
		"legal"
	],
	"user" : {
		"name" : "Manuel",
		"status" : "author"
	},
	"date" : "Thu Aug 22 2019 13:01:24 GMT+0800 (PST)"
}

Pretty!

 Inserting a Row in MongoDB

Inserting a row is a very fundamental action in databases. MongoDB makes it very easy.

First you need to be in your database

> use your_database_name
switched to db your_database_name

Insert your record using the insert method on your collection

> db.books.insert({
  title: 'Book Title',
  category: 'Fiction',
  tags: ['children', 'fantasy'],
  user: {
    name: 'Sophia',
    status: 'author'
  },
  date: Date()
})
WriteResult({ "nInserted" : 1 })

Show your rows

> db.books.find()
{ "_id" : ObjectId("5d5e1c357545de885fa391fb"), "title" : "Book Title", "category" : "Fiction", "tags" : [ "children", "fantasy" ], "user" : { "name" : "Sophia", "status" : "author" }, "date" : "Thu Aug 22 2018 12:38:12 GMT+0800 (PST)" }

Awesome!

 Create a Collection in MongoDB

A Collection is comparable to a table in a relational database. It is easy to create a collection in MongoDB.

First you need to be in your database

use your_database_name
switched to db your_database_name

Create your collection

db.createCollection('books')
{ "ok" : 1 }

Show your collection

> show collections
books

Easy

 Create a Database in MongoDB

MongoDB is one of the most popular NoSQL databases around. The popularity of NoSQL databases boomed with the MEAN and MERN.

Creating a database is really simple especially in MongoDB.

use your_database_name
switched to db your_database_name

It is really that simple. There is one bit of a problem here. When you use the show dbs command, you won’t see any new databases. This is because the new database doesn’t have any collection yet.

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Let’s create our collection

> db.createCollection('books')
{ "ok" : 1 }
> show dbs
admin               0.000GB
config              0.000GB
local               0.000GB
your_database_name  0.000GB

Now that’s how to create your database in MongoDB.