![]() |
VOOZH | about |
MongoDB is a popular NoSQL database designed for handling large volumes of diverse data types. Its flexible schema, high performance and scalability make it ideal for modern applications requiring rapid development and efficient data management.
In this article, We will learn about the MongoDB Terminology in detail.
All modern applications require Big Data, faster development, and flexible deployment. This is satisfied by the document-based database like MongoDB.
Data is a raw or unprocessed information. example: marks, class, etc.
Database is a collection of physically stored data into computer. Data is primarily stored on the secondary storage. Data stored in the form of tables. Software that provide a way to manage this data called Database Management System (DBMS).
Aggregation is important function of MongoDB. Aggregation is the operation which groups the data from multiple destinations and perform some operations onto for produce some result. It is similar to the count(*) and Group By Clause in SQL. Aggregation framework provided by MongoDB is as follow:
Some Aggregation Functions are Listed Below:
Expression | Description |
|---|---|
Sums the defined values from all the documents in a collection | |
$avg | Calculates the average values from all the documents in a collection |
Return the minimum of all values of documents in a collection | |
$max | Return the maximum of all values of documents in a collection |
Inserts values to an array but no duplicates in the resulting document | |
Inserts values to an array in the resulting document | |
$first | Returns the first document from the source document |
$last | Returns the last document from the source document |
Here, You can find more about the Aggregation in MongoDB
Example of Aggregation: Sum of all marks of students:
db.students.aggregate([
{
$group: {
_id: null,
totalMarks: { $sum: '$marks' },
},
},
]);
Output:
Index is a special type of data structure that stores some information about database so that we can retrieve the information efferently by querying.
Traditionally, without the indexing if we query the database the default index finds the collection based on _id field. this method is not efficient and hence to retrieve the information more efficiently and fast we implement Indexing on database. We can Create the index, Drop the index and get description of index.
Creating Index:
db.students.ensureIndex({'marks':1});
Output:
Explanation: marks is the key name that you want to make an index and it will be 1 and -1 to make it on ascending or descending order
Drop Index:
db.students.dropIndex("marks_1")
Output:
Here, you can find more about indexing in MongoDB.
Here, You can find more on Sharding in MongoDB.
Following chart shows the relationship between SQL terminologies and MongoDB terminologies
A collection is equivalent to an RDBMS table. It store number of documents inside it. Since MongoDB is schema less, collection do not have schemas. We can store any number of documents in single collection and all of them have related purpose.
You can create a collection using the createCollection() database method.
Syntax :
db.createCollection("collection_name")Output:
You can also create a collection while inserting document:
db.collection_name.insertOne(document_object)It will create if not already exists.
To show all the collections use show collections comman
It is a basic unit of data in MongoDB. document is set of key value pairs. It is a record which is being inserted in the collection. It may be already existing data in the collection. we insert documents in the form of JSON. document can have flexible schema that means different documents in collection do not need to have same set of fields or structure.
We can insert document in the collection using two methods:
Example for insertOne():
db.students.insertOne({name:"Jayesh", age:21, rollno:77, marks:97})Qutput:
Example for insertMany()
findOne(): To show all the documents use findOne() method in which you can pass the key value pair of the desired document.
Field is key-value pair which is base of inserting data into documents. documents can have any number of fields. it can have zero or more fields. it is same as columns in RDBMS.
Syntax:
{key:value}Let's understand concept of fields with help of example, given is the output of findOne() method.
Explanation: In this document there are different fields present such as name, age , rollno, marks. _id is the special field which is assigned automatically by MongoDB server. Hence, the fields are the key value pairs in the documents.
_id is a special key present in the document. it is used to uniquely identify the document. _id is automatically generated by the MongoDB. we can't change the value to this field.
Explanation: Here as we can see _id: 65849d677de3dc96745d21d5 is automatically generated. we haven't included _id field while inserting document.
Overall, We have also seen that the need of MongoDB and some basic terms which are frequently used in MongoDB to perform operations. We sum each and every terminology with the example also compared the MongoDB terminologies with the RDBMS terminologies. Mongodb is a NoSQL database which is used to handle complex, large and uncomplicated data in very easy way.