Introduction To MongoDB

Yasith Wimukthi
4 min readMar 10, 2022

MongoDB is a cross-platform, document-oriented database with excellent speed, high availability, and automatic scaling. MongoDB is based on the collection and document concepts. It is a NoSQL database. MongoDB is freely accessible under the GNU General Public License, and it is also available from the manufacturer under the Commercial license. MongoDB’s development began in 2007 when the business was developing a platform as a service comparable to Windows Azure.

NoSQL Databases

There are three types of databases.

  1. RDBMS (Relational Database Management System)
  2. OLAP (Online Analytical Processing)
  3. NoSQL

NoSQL Database is used to refer non-SQL or non relational databases. It provides an alternative mechanism for data storage and retrieval to the tabular relations model used in relational databases. Tables are not used to store data in a NoSQL database. It is commonly used to store large amounts of data as well as real-time web applications.

Advantages of NoSQL

  • It supports query language.
  • It provides fast performance.
  • It provides horizontal scalability.
  • Handle large volumes of data at high speed with a scale-out architecture
  • Store unstructured, semi-structured, or structured data
  • Enable easy updates to schemas and fields
  • Developer-friendly

MongoDB Advantages

  • MongoDB has no schema. It is a document database in which each collection has a separate set of documents.
  • The amount of fields, content, and size of the document may fluctuate from one to the next.
  • In MongoDB, the structure of a single object is obvious.
  • There are no complex joins.
  • MongoDB allows deep queries since it has a powerful dynamic query on documents.
  • easy to scale.
  • It uses internal memory for storing working sets therefore it has a fast access.

Collection

Collection is a group of MongoDB documents. It is analogous to an RDBMS table. A collection exists within a single database. A schema is not enforced by collections. Different fields can be assigned to documents inside a collection. Typically, all documents in a collection serve the same or comparable purpose.

Document

A document consists of a collection of key-value pairs. Dynamic schema exist in documents. Documents in the same collection do not have to have the same set of fields or structure, and common fields in a collection’s documents might contain different types of data. Following example shows a example document structure.

{
_id: ObjectId(df78e7rgav5s41bg56e4)
name: { first: "Alan", last: "Turing" }
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}

MongoDB CRUD Operations

1. Create Operations

Insert or create operations Add new documents to an existing collection. Insert actions will create the collection if it does not already exist .MongoDB provides the following methods to insert documents into a collection:

  • db.collection.insertOne()
  • db.collection.insertMany()

Insert a Single Document

db.collection.insertOne() inserts a single document into a collection.

The following example inserts a new document into the book collection. If the document does not specify an _id field, MongoDB adds the _id field with an ObjectId value to the new document.

db.book.insertOne( 
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
}
)

Insert Multiple Documents

db.collection.insertMany() can insert multiple documents into a collection. If you want to insert multiple documents in a collection, you have to pass an array of documents to the db.collection.insertMany() method.

db.book.insertMany([ 
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
},
{
name: “JavaScript The Definitive Guide ”,
Author: David Flanagan,
price: 200.00,
edition: 7,
tags: ["Javascript","Ecmascript"]
},
{
name: “Modern JavaScript for the Impatient ”,
Author: Cay S. Horstmann,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
}
])

2. Read Operations

Read operations retrieve documents from a collection. db.collection.find() method is used to retrieve documents from a collection. This method returns a cursor to the retrieved documents. To retrieve all documents from a collection, put the query document ({}) empty.

db.collection.find()

The following example selects from the book collection all documents where the name equals “JavaScript The Definitive Guide”:

db.book.find( { name: "JavaScript The Definitive Guide" } )

3. Update Operations

Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection.

  • db.collection.updateOne() : Update a single document that satisfies a condition.
db.collection.updateOne(filter, update, options)db.boook.updateOne({_id:1}, { $set: {name:'Javascript Cookbook'}})
  • db.collection.updateMany() : Updates all documents that match the specified filter for a collection.
db.collection.updateMany(filter, update, options)db.boook.updateMany({name:'Javascript Cookbook'}, { $set: {price: 300.00}})
  • db.collection.replaceOne() : Replaces a single document within the collection based on the filter.
db.collection.replaceOne(filter, replacement, options)db.boook.replaceOne(
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 100.00,
edition: 3,
tags: ["Javascript","Ecmascript"]
},
{
name: “JavaScript Cookbook ”,
Author: Adam D. Scott,
price: 200.00,
edition: 4,
tags: ["Javascript","Ecmascript"]
}
);

4. Delete Operations

Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:

  • db.collection.deleteOne() : Removes a single document from a collection.
db.collection.deleteOne(filter, option)db.book.deleteOne({ _id: 1 })
  • db.collection.deleteMany() : Removes all documents that match the filter from a collection.
db.collection.deleteOne(filter, option)
db.book.deleteMany({ "price": { $gt: 200.00 } })

--

--