Dino Geek, try to help you

How to use MongoDB with Java?


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:

```
org.mongodb mongodb-driver 3.12.6

```

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 collection = db.getCollection(“testCollection”); System.out.println(“Collection selected successfully”); } catch (Exception e) { e.printStackTrace(); } }
}
```

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 collection = db.getCollection(“testCollection”);

Document document = new Document(“title”, “MongoDB”) .append(“id”, 1) .append(“description”, “database”) .append(“likes”, 100) .append(“url”, “http://www.mongodb.com/”) .append(“by”, “Flynn”); collection.insertOne(document); System.out.println(“Document inserted successfully”); } catch (Exception e) { e.printStackTrace(); } } } ```

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.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use