List of Most Common Commands in MongoDB Architecture in 2021

 It is common to know, there are 2 types of databases SQL and NoSQL. Hence in this blog post, we will be discussing NoSQL database that is MongoDB. It is a document-type database storing data in the form of JSON-like documents. One of the major reasons for its popularity is the data model used as a highly elastic one allows combining and storing of data of multivariable types.

MongoDB Architecture

Before discussing commands it is important to learn the architecture of MongoDB. It is a combination of 3 parts:

1. Database

A database is basically a physical container for data. Each database comprise of multiple files on the file system. In addition, multiple databases can exist on a single MongoDB server.

2. Collection

A collection is basically a collection of database documents. This is like a table in SQL and its biggest advantage is that there is no need to mention schemas when it is about collections. In a single collection, multiple documents can have different fields.

3. Document

Document is basically a key-value pair. It includes dynamic schemas allowing them to have flexible content.

 



MongoDB Commands

With a brief introduction about the architecture of MongoDB, let’s find out the important commands on how to build the database and query it. These commands are used by MongoDB Development Company to build secure & robust database.

1. Database Creation

MongoDB is not integrated with any specific command to create a database. But it supports the development of collection. It simply creates a database after the values are saved into the collection for the very first time. The command discussed below will create a database named ‘database’ if it is not present. If it does exist, then it will be selected.

Command: Use database

2. Dropping Databases

The command discussed below is used to drop a database. This command works on the database currently used.

Command: db.dropDatabase()

3. Creating a Collection

This command is used to create a collection. This is also an optional command used to createa collection automatically when data is inserted for the very first time.

Command: db.createCollection(name, options)

Name: The string type mentions the name of the collection to be created.

Options: The document type displays the memory size including the indexing of the collection. It is an optional parameter.

4. Showing Collections

The following command is used to display all collections.

Command: db.getCollectionNames()

5. Projection

A number of times only specific parts of the database are needed instead of the whole database.

Find() method is used to display all fields of a document. For choosing specific fields, a list of fields having value 1 or 0 is passed. 1 is for showing the field and 0 is for hiding it. This ensures that only those fields with value 1 are selected.

Command: db.COLLECTION_NAME.find({},{KEY:1})

6. Date Operator

This command is used to adddate and time to the database.

Command:

Date() – It returns the current date as a string.

New Date() – It returns the current date as a date object.

7. $not Operator

$not does a logical NOT operation on the specified <operator-expression> and selects only those documents that don’t match the <operator-expression>. This only includes those documents which do not have that particular field. Similar is the AND and OR commands.

Command: { field: { $not: { <expression> } } }

8. Delete Commands

The below discussed commands are used to delete operations-

Commands:

collection.remove() – It deletes a single document that matches a filter. db.collection.deleteOne() – It deletes only the first matched document even if the command selects more than one document.

db.collection.deletemany() – It deletes all the documents that match the specified filter.

So, these are the important commands of MongoDB database integrated when you avail the best MongoDB Development Services for your business enterprise. These commands lay a vital role in design & development of database.

Wrapping Up:

These are the most common commands used by database administrators when working with MongoDB. Implementing them within the database brings remarkable results and helps to perform task at a fast rate.

Comments

Popular posts from this blog

MongoDB vs Firebase - Which is the Best Database for You in 2021?

5 Things to Consider for MongoDB Deployment Preparation

Why Choose MongoDB Development Services for Your Web Application?