Learn How to Update and Delete From a Collection using MongoDB

0
3217
Collection using MongoDB

Collection using MongoDB

In this chapter, we are going to use the MongoDB JDBC connection (explained in the previous article) in order to connect MongoDB database with JAVA. After successfully connecting MongoDB with JAVA, we are going to create a collection, insert, modify and delete documents into this collection through JAVA programming language.

MongoDbJdbcConnection.java

MongoDB – Creation, Selection and modification of a document in a collection
Using the class ‘MongoDbJdbcConnection.java’, we will be able to connect to MongoDB by simply calling the static method getMongoDBConnection which accepts three arguments i.e. host name, port number and database name. This method will return a DB class object. By using this DB object, we are going to first create and select a collection followed by the modification of a document in this collection through JAVA Programming.

Syntaxes

  • To create a collection, we can use the ‘createCollection ()’ method of com.mongodb.DB class.
  • To get or select a collection from the database, we can use the ‘getCollection ()’ method of com.mongodb.DBCollection class.
  • To insert a document into MongoDB, we can use the ‘insert ()’ method of com.mongodb.DBCollection class.
  • To modify or update a document into MongoDB, we can use the ‘update ()’ method of com.mongodb.DBCollection class.

MongoDbUpdateDemo.java

Before executing this program, make sure that the MongoDB instance is running on your local or remote machine. Here, the MongoDB instance is running on the local machine at port 27017.

Explanation of Code

  • Firstly, we are connecting the MongoDB client by passing three arguments to the static getMongoDBConnection method of ‘MongoDbJdbcConnection.java’ class. This will return a DB class instance of a MongoDB database into ‘db’ instance variable.
  • Next, we are creating instance of ‘DBObject’ through ‘BasicDBObject’ class that helps to create the schema definition to be inserted in the collection.
  • Next, we are creating collection through ‘db.createCollection (“MyCollection10”, dbObject);’ method.
  • Next, we are going to select this collection ‘MyCollection10’ through ‘db.getCollection (“MyCollection10”);’ method which returns the instance into ‘coll’ instance variable.
  • Through ‘coll’ instance variable, we can use ‘coll.insert (dbObject);’ to insert the DBObject instance which we have created before. At this step, a document has successfully inserted into the MongoDB.
  • Now, we are using ‘DBCursor’ class which returns the cursor to iterate over the documents and display those documents present in the current collection through ‘DBCursor cursor = coll.find ();’ method.
  • Next, we are again using ‘DBCursor’ class which returns the cursor to iterate over the documents and modify or update those documents present in the current collection through ‘coll.update (dbObjectQuery, updateDocument)’ method.
  • Next, we are again using ‘DBCursor’ class which returns the cursor to iterate over the documents and display the modified documents present in the current collection through ‘DBCursor cursor = coll.find ();’ method.

Output
When we execute above JAVA program, it will connect to the MongoDB and do the creation, selection and retrieving operations on the collection ‘MyCollection10’ followed by the modification of a document present in this collection as shown on the console.

MongoDB – Deletion of a document in a collection
Using the class ‘MongoDbJdbcConnection.java’, we will be able to connect to MongoDB by simply calling the static method getMongoDBConnection, which accepts three arguments i.e. host name, port number and database name. This method will return DB object. By using this DB object, we are going to select a collection followed by the deletion of a document from this collection through JAVA Programming.

Syntaxes

  • To get or select a collection from the database, we can use the ‘getCollection ()’ method of com.mongodb.DBCollection class.
  • To delete a document into MongoDB, we can use the ‘remove ()’ method of com.mongodb.DBCollection class.

MongoDbDeleteDemo.java

Before executing this program, make sure that the MongoDB instance is running on your local or remote machine. Here, the MongoDB instance is running on the local machine at port 27017.

Explanation of Code

  • Firstly, we are connecting the MongoDB client by passing three arguments to the static getMongoDBConnection method of ‘MongoDbJdbcConnection.java’ class. This will return a DB class instance of a MongoDB database into ‘db’ instance variable.
  • Next, we are going to select this collection ‘MyCollection10’ through ‘db.getCollection (“MyCollection10”);’ method which returns the instance into ‘coll’ instance variable.
  • Next, we are finding a document through coll.findOne () method and calling the remove () method to delete this document from the selected selection.
  • Now, we are using ‘DBCursor’ class which returns the cursor to iterate over the documents and display those documents present in the current collection through ‘DBCursor cursor = coll.find ();’ method.

Output
When we execute above JAVA program, it will connect to the MongoDB and perform the select operation on the collection ‘MyCollection10’ followed by the deletion of a document present in this collection as shown on the console.

Source code for the Update and Delete From a Collection using MongoDB

Conclusion: –
In this tutorial, we have created a JDBC connection to MongoDB database through a static method of a generic class that accepts connection arguments. After JDBC connection, we have created a collection to do the modification and deletion operation on a document present in a collection of MongoDB.

LEAVE A REPLY

Please enter your comment!
Please enter your name here