MongoDB is a no-SQL database that is document-oriented. MongoDB, which was first released in 2009, successfully replaced rows and columns in traditional relational data models with documents.
We do not need to use the JDBC API to connect to MongoDB because its API is completely independent and does not require any other external library.
The MongoClient class connects to a MongoDB server and performs database-related operations. Here are a couple of examples:
MongoClient mongoClient = new MongoClient();
Connecting to a named MongoDB server listening on the default port (27017):
MongoClient mongoClient = new MongoClient("localhost");
Connecting to a named MongoDB server listening on a specific port:
MongoClient mongoClient = new MongoClient("localhost", 27017);
After you’ve established a connection, you can use the getDatabase() method to retrieve a database by name.
for instance,
MongoDatabase db = mongoClient.getDatabase()("test");
Obtaining collections is a necessary step in querying and manipulating data. The getCollection() allows you to retrieve a collection by name.
MongoCollection<Document> collection = database.getCollection("testCollection");
Use the Document class and its append() method to add additional fields and values to the document object when creating the document with the Java driver.
example:
BasicDBObject doc = new BasicDBObject("name", "MongoDB").
append("type", "database").
append("count", 1).
append("info", new BasicDBObject("x", 203).append("y", 102));
After creating a document, we can add it to the collection. We can also insert single or multiple documents. The insertOne() method of the collection is used to insert a single document into it.
For example:
coll.insert(doc);
The find() method can be used to retrieve a single document from a collection.
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class Demo {
public static void main(String[] args){
try{
// Connecting To MongoDB
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// Creating DataBase
MongoDatabase db = mongoClient.getDatabase("test");
// Creating Collection/Table
MongoCollection<Document> table = database.getCollection("testCollection");
// Creating Document/Record
BasicDBObject doc = new BasicDBObject("name", "MongoDB").
append("type", "database").
append("count", 1).
append("info", new BasicDBObject("x", 203).append("y", 102));
// Inserting Data
table.insertOne(doc);
}catch(Exception e){
System.out.println(e);
}
}
}
Note: also read about the Java connect to MySQL database with JDBC
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…