Before you query data from MongoDB, you would need to have knowledge on the three core concepts:
1. Database
2. Collection
3. Document
After you understand these concepts, you can start using MongoDB with Java.
Step 1: MongoDB Java Driver
To connect MongoDB in Java, first we need to include its driver, you can do so by Maven or by manually downloading the .jar file and adding it to your class path.
Maven Dependency for the MongoDB Java Driver:
```
```
Step 2: Setting Up MongoDB Connection
```
import com.mongodb.*;
import com.mongodb.client.MongoDatabase;
public class MongoConnect {
public static void main(String[] args) {
try {
MongoClient mongoClient = new MongoClient( “localhost” , 27017 );
System.out.println(“Connect to mongodb successfully”);
MongoDatabase db = mongoClient.getDatabase(“testDB”);
System.out.println(“Database selected successfully”);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
Step 3: Create Collection
```
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongoConnect {
public static void main(String[] args) {
try {
MongoClient mongoClient = new MongoClient(“localhost”, 27017);
System.out.println(“Connect to mongodb successfully”);
MongoDatabase db = mongoClient.getDatabase(“testDB”);
MongoCollection
}
```
Step 4: Insert Document
```
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongoConnect {
public static void main(String[] args) {
try {
MongoClient mongoClient = new MongoClient(“localhost”, 27017);
MongoDatabase db = mongoClient.getDatabase(“testDB”);
MongoCollection
These are the fundamental steps to connect and start inserting data into MongoDB using Java. There are many other operations you can perform such as updating, deleting and querying data. You can find all these in the official MongoDB’s Java driver documentation.