![]() |
VOOZH | about |
MongoDB is an open-source cross-platform document database developed using C++. Some features of MongoDB are:
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:
Note: Make sure to install and setup MongoDB JDBC driver and Java.
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.
The following code establishes a connection to MongoDB ->
Output: 👁 Image
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:
Output: 👁 Image
For getting a collection, MongoCollection.getCollection() method is used. Below is the implementation of this approach:
Output 👁 Image
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:
Output: 👁 Image
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:
Output: 👁 Image
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:
Output: 👁 Image
For deleting the document, deleteOne() method is used. Following is the code for deleting the document ->
Output: 👁 Image
"Collection.drop()" is used to drop the created collection. Following is the code for dropping the collection:
Output: 👁 Image
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:
Output: 👁 Image