- High and effective performance
- Easily scalable
- High availability
- It can store high volume of data
It contains data in the form of collections and documents instead of rows and tables. A collection is a set of documents. The collection does not have schemas. It represents data in the form of a hierarchical model with which the storage of arrays and other data structures will be easy.
Components of MongoDB
The essentials components of MongoDB are listed below:
- id: This field represents a unique field in MongoDB. This field is created by default.
- Collection: It is a set of MongoDB documents. It exists with a single database.
- Database: This is the container for collections. Multiple databases can be stored in a mongoDB server.
- Document: A record in mongoDB is called a document. It containes names and values.
- Field: It is a name-value pair in a document.
Table of contents: 1. Establishing connections to database 2. Creating a MongoDb collection 3. Getting a Collection 4. Inserting Values into MongoDb 5. Displaying the list of all Documents 6. Updating documents in the MongoDB 7. Deleting a Document 8. Dropping of a Collection 9. Displaying all the collections
For making the connection, you have to mention the database name. MongoDB creates a database by default if no name is mentioned.
- Firstly, import the required libraries for establishing the connection.
- Here, “MongoClient” is used to create the client for the database.
- “MongoCredential” is used for creating the credentials.
- And finally, to access the database “MongoDatabase” is used.
- Username will be: “GFGUser” and the database name will be “mongoDb“.
- The function “.toCharArray()” is used to convert the password into a character array.
- The function “.getDatabase()” is used for getting the database.
The following code establishes a connection to MongoDB ->
To create a collection com.mongodb.client.MongoDatabase class and createCollection() method is used. Here, “database.createCollection()” creates a collection named as “GFGCollection”. Following is the code for creating collection:
For getting a collection, MongoCollection.getCollection() method is used. Below is the implementation of this approach:
Only a document type of data can be inserted a MongoDB. Therefore, either we can create a document with the values to be inserted using append() method or pass a document directly into the MongoDB using .insert() method.
Here, first, we created a new document as “title” and then append the “about” section. Then, we have given the respective values to the documents. The function “.insertOne()” is used to insert the document into the collection.
Below is the implementation of this approach:
For displaying all documents of collection, find() method is used.
Here, the database has two documents namely “document” and “document1”, which are retrieved using find() method. We use an iterator since it will iterate over each document present in the list and display it to us.
Following is code for displaying all the documents:
For updating the document, updateOne() method is used.
Here, “Filters.eq” creates a filter that matches all documents with the name provided as argument. “Updates.set()” is used to update the document as the given value in the argument.
Following is the code for it:
For deleting the document, deleteOne() method is used. Following is the code for deleting the document ->
“Collection.drop()” is used to drop the created collection. Following is the code for dropping the collection:
For displaying the list of all collections, listCollectionNames() method is used.
Here, we iterate over all the collections we created with the help of “for()” statement. Database.listCollectionNames() is used to display the list of all collections present in the database.
Following is the code for displaying all the collections:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java Tutorial
- Upload and Retrieve Image on MongoDB using Mongoose
- MongoDB - Comparison Query Operators
- MongoDB - Logical Query Operators
- MongoDB - Field Update Operators
- MongoDB - Multiply Operator ($mul)
- MongoDB - Rename Operator ($rename)
- MongoDB - Current Date Operator ($currentDate)
- MongoDB - SetOnInsert Operator ($setOnInsert)
- MongoDB - Positional Operator ($)
- MongoDB - $pull Operator
- MongoDB - $each Modifier
- MongoDB - $push Operator
- MongoDB - $pullAll Operator
- MongoDB - $pop Operator
- MongoDB - $sort Modifier
- MongoDB - $slice Modifier
- MongoDB - $position Modifier
- Difference Between Hadoop and MongoDB
- Native MongoDB driver for Node
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.