MongoDB is a popular open-source document database and NoSQL database. MongoDB was created using the C++ programming language. MongoDB is a document-oriented database that provides ease of scaling, higher availability, and higher performance. It operates on a number of systems and platforms.
MongoDB is built on concepts of documents and the principles of collection. On a single MongoDB server, many databases are frequently discovered. (A physical location where data is stored is called a Database. Each database has its set of files on the file system.) In this article, we will be discussing, in short, almost every topic related to MongoDB.
The Embedded data model and Normalized data model are the two types of data models offered by MongoDB.
This paradigm, also known as the de-normalized data model, allows us to have (embed) all associated data in a single document.
Normalized Data Modeling
We can use references in this model to refer to subdocuments in the primary document.
studDocID: " ObjectId121",
studDocID: " ObjectId123",
This has two methods - limit() method and skip() method.
The limit() method in MongoDB is used to limit the number of records. The method takes only one number type argument, which is the number of documents to display.
Aside from the limit() function, there's also skip(), which takes a number type input and skips the specified number of documents.
In MongoDB, the sort() method is used to sort documents. The method takes a document with a list of fields and the order in which they should be sorted. The numbers 1 and -1 are used to determine the sorting order. In ascending order, 1 is utilized, while in descending order, -1 is used.
Syntax of sort() method
We use the use command to create a database in MongoDB.
The use Command
DATABASE NAME is the name of the database created by MongoDB. If a database does not exist, the command will create one; otherwise, it will return the current database.
If you wish to learn about creating a database in MongoDB, read this comprehensive article.
To delete an existing database in MongoDB, use the db.dropDatabase() command.
To construct a collection in MongoDB, use the db.createCollection(name, parameters) method.
db.collection is a MongoDB collection. db.collection.drop() is a function that is used to remove a collection from the database.
Capped Collection in MongoDB
Updates to documents that result in increased document size are restricted in capped collections. Capped collections hold documents in the same order as disc storage, ensuring that the document size does not exceed the disc space allocated.
Creating Capped Collection
To make a capped collection, use the createCollection command with the capped option set to true and the collection's maximum size in bytes specified.
Querying Capped Collection
A find query on a capped collection will return results in insertion order by default. Use the sort command instead if you want the documents to be returned in reverse order.
Data Types in MongoDB
- Double - Floating-point values are stored using the double type.
- Timestamp - This is useful for keeping track of when a document has been edited or added to.
- Max/Min keys - This type of key is used to compare a value to the BSON elements with the lowest and highest values.
- String - The most often used datatype for storing data is a string. In MongoDB, strings must be valid in UTF-8.
- Symbol - This datatype is used in the same way as a string, although it's usually reserved for languages that utilize a specific symbol type.
- Binary - The datatype binary data is used to store binary data.
- Integer - Integer is a data type that stores a numerical value. Depending on your server, integers can be 32-bit or 64-bit.
- Regular Expression - The datatype regular expression is used to store regular expressions.
- Arrays - This type is used to hold numerous values in one key, such as an array, a list, or a list of values.
- Date - The current date or time in UNIX time format is stored in this data type. You can create your own date time by constructing a Date object and passing in the day, month, and year.
- Null - This type is used to store a value that is null.
- Boolean - The boolean (true/false) value is stored using this type.
- Object - Object This data type is used for documents that are embedded.
- Object ID - It is a data type for storing the document's ID.
Projection in MongoDB
In MongoDB, projection refers to not picking the complete document's information but information that is actually required. If there are five fields in a document and we need to see only 3 of them, then we get to choose only three of them.
The find() method in MongoDB allows a second optional parameter, which is a list of fields to obtain, as defined in the MongoDB Query Document. All of a document's fields are displayed whenever MongoDB's find() method is used. Providing a list of fields with the values 0 or 1 will prevent this from happening. The value 0 indicates that the field should be hidden, whereas 1 indicates that the field should be visible.
MongoDB Insert Document
We need to use MongoDB's insert() or save() methods to insert data into a MongoDB collection.
MongoDB Delete Document
To delete a document from a collection, use MongoDB's remove() method. The delete() function takes two arguments. The first is the deletion criteria, while the second is the justOne flag.
deletion criteria - It is based on documents (optional) Delete criteria based on documents will be removed.
justOne(optional) - If set to true or 1, justOne removes only one document.
MongoDB Update Document
To update a document into a collection, use MongoDB's update() and save() functions. The save() function replaces the existing document with the document supplied in the save() method, while the update() method updates the values in the existing document.
The update() method modifies the existing document's values.
The save() method overwrites the existing document with the new one that is handed in.
Indexes are unique data structures that store a tiny fraction of the data set in a format that is easy to navigate. The value of a specific field or collection of fields is stored in the index, which is ordered by the field's value as stated in the index.
MongoDB's createIndex() method is used to create an index.
MongoDB's dropIndex() method allows you to remove a specific index.
This function deletes a collection's numerous (specified) indexes.
The description of all the indexes in the collection is returned by this method.
Sharding in MongoDB
MongoDB's method of handling the needs of data growth is sharding, which is the technique of storing data records across numerous machines. As data grows in size, a single system may not be able to store it all or provide a satisfactory read and write throughput. The difficulty of horizontal scaling is solved by sharding.
These Have Three Components in MongoDB:
1. Query Routers: Mongo instances that communicate with client apps and route operations to the appropriate shard are called Query Routers. The query router runs the operations, targets them to shards, and then returns the results to the clients. To share the client request load, a shared cluster can include many query routers.
2. Config servers: These keep track of the metadata for the cluster. This data contains a mapping of the cluster's data set to the shards. The query router uses this metadata to target operations to certain shards. In production, shared clusters contain three config servers.
3. Shards: They are a type of data storage. They give strong data consistency and availability. Each shard is a single duplicate set in the production environment.
Regular Expressions are commonly used to search for a pattern or word in a string in all languages. The $regex operator in MongoDB also has regular expression capability for string pattern matching. MongoDB's regular expression language is PCRE (Perl Compatible Regular Expression).
Enroll in the PG Program in Data Science to learn over a dozen of data science tools and skills, and get exposure to masterclasses by Purdue faculty and IBM experts, exclusive hackathons, Ask Me Anything sessions by IBM.
Level Up Your Data Science Career With Simplilearn
In this article, we discussed most of the important topics related to MongoDB in brief and got a complete overview of each of those. To dive deep into this and get a grasp of each of these topics and more in a detailed manner, check out Simplilearn’s Data Science Certification Bootcamp to level up your data science career.