All you need to know about MongoDB 3.2

What is MongoDB?

MongoDB is an open-source document database that provides high performance, high availability, and also automatic scaling. An open-source database was released in 2009. Now it is available under the Free Software Foundation’s GNU AGPL Version 3.0 commercial license terms.

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are like JSON objects. The values of fields may include other documents, arrays, and also with arrays of documents. If you are new to MangoDB, you can get the detailed comparison between Firebase, AWS and MongoDB at- Three Modern Technologies Software Stacks: Firebase vs. AWS vs. MongoDB.

The advantages of using documents are:

  • Documents (i.e. objects) correspond to native data types in many programming languages.
  • Embedded documents and arrays reduce need for expensive joins.
  • Dynamic schema supports fluent polymorphism.

Key Features of MongoDB-

1. High Performance-

MongoDB provides high performance data persistence. In particular,

  • Support for embedded data models reduces I/O activity on database system.
  • Indexes support faster queries and can include keys from embedded documents and arrays.

2. Rich Query Language

MongoDB supports a rich query language to support read and write operations (CRUD) and also:

  • Data Aggregation
  • Text Search and Geospatial Queries.

3. High Availability-

A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and also increasing data availability.

MongoDB’s replication facility is known as replica set. It provides:

  • automatic failover and
  • data redundancy.

4. Horizontal Scalability-

MongoDB provides horizontal scalability as part of its core functionality:

  • Sharding distributes data across a cluster of machines.
  • Tag aware sharding allows for directing data to specific shards, such as to take into consideration the geographic distribution of the shards.

5. Support for multiple storage engine-

MongoDB supports multiple storage engines:

  • WiredTiger Storage Engine (including support for Encryption at Rest)
  • In-Memory Storage Engine and also
  • MMAPv1 Storage Engine

In addition, MongoDB provides pluggable storage engine API that allows third parties to develop storage engines for MongoDB.

6. MongoDB CRUD Operation-

1. Create Operations-

Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection. MongoDB provides some methods to insert documents into a collection. These methods are as follows-

  • db.collection.insert()-

To insert a single document, pass a document to the method, and also to insert multiple documents, pass an array of documents to the method. The following example inserts a new document into the users collection.

db.users.insert(

   {

      name: “sue”,

      age: 19,

      status: “P”

   }

)

The following example inserts multiple documents into the users collection. Since the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document. See Insert Behavior.

db.users.insert(

   [

     { name: “bob”, age: 42, status: “A”, },

     { name: “ahn”, age: 22, status: “A”, },

     { name: “xi”, age: 34, status: “D”, }

   ]

)

  • db.collection.insertOne() New in version 3.2-

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

The following example inserts a new document into the users collection. The new document has three fields name, age, and status. Since the document does not specify an _id field, MongoDB adds the _id field with an ObjectId value to the new document.

db.users.insertOne(

   {

      name: “sue”,

      age: 19,

      status: “P”

   }

)

  • db.collection.insertMany() New in version 3.2

db.collection.insertMany() inserts multiple documents into a collection.

db.users.insertMany(

   [

     { name: “bob”, age: 42, status: “A”, },

     { name: “ahn”, age: 22, status: “A”, },

     { name: “xi”, age: 34, status: “D”, }

   ]

)

2. Read Operation-

Read operations retrieves documents from a collection; i.e. queries a collection for documents. MongoDB provides the following methods to read documents from a collection:

  • db.collection.find()

You can specify query filters or criteria that identify the documents to return.

db.users.find(

{ age: { $gt: 18 } },

{ name: 1, address: 1 }

) limit(5)

3. Update Operations- 

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

  • db.collection.update()
  • db.collection.updateOne() New in version 3.2
  • db.collection.updateMany() New in version 3.2
  • db.collection.replaceOne() New in version 3.2

In MongoDB, update operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

You can specify criteria, or filters that identify the documents to update. These filters use the same syntax as read operations.

For eg.,

db.users.update(

{ age : { $gt : 18 } },

{ $set : { status : “A” } },

{ multi : true }

)

4. Delete Operations- 

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

  • db.collection.remove()
  • db.collection.deleteOne() New in version 3.2
  • db.collection.deleteMany() New in version 3.2

In MongoDB, delete operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

For eg. , db.users.update(

{ status : “D” }

)

7. Storage-

The storage engine is the primary component of MongoDB responsible for managing data. MongoDB provides a variety of storage engines. This allows you to choose the one most suited to your application. The journal is a log that helps the database recover in the event of a hard shutdown. There are several configurable options that allows the journal to strike a balance between performance and also reliability that works for your particular use case. For eg., GridFS is a versatile storage system suited for handling large files.

8. Authentication-

Authentication is the process of verifying the identity of a client. When access control, i.e. authorization, is enabled, MongoDB requires all clients to authenticate themselves in order to determine their access. Although authentication and authorization are closely connected, authentication is distinct from authorization. Authentication verifies the identity of a user; authorization determines the verified user’s access to resources and operations.

  •  Use the mongo command-line authentication options (–username, –password, and –authenticationDatabase) when connecting to the mongod or mongos instance, or
  • Connect first to the mongod or mongos instance, and then run the authenticate command or the db.auth() method against the authentication database.

9. Encryption- 

Transport Encryption-

You can use TLS/SSL (Transport Layer Security/Secure Sockets Layer) to encrypt all of MongoDB’s network traffic. TLS/SSL ensures that MongoDB network traffic is only readable by the intended client. There are two broad classes of approaches to encrypting data at rest with MongoDB: Application Level Encryption and Storage Encryption. You can use these solutions together or independently.

10. Auditing-

The auditing facility can write audit events to the console, syslog, a JSON file, or a BSON file.

Audit Events and Filter-

Once enabled, the auditing system can record the following operations:

  • schema (DDL),
  • replica set and sharded cluster,
  • authentication and authorization, and
  • CRUD operations (requires auditAuthorizationSuccess set to true).

Are you looking to develop software with MongoDB? Then you are at the right place. Solace team is there to help you. Experts at solace believe in the effectiveness of using MongoDB. Develop your best software with solace that means to the success that your business deserves. Contact us for any software development.