Ein einfaches Java + MongoDB Hallo Welt-Beispiel - wie man eine Verbindung herstellt, eine Datenbank erstellt, ein Dokument sammelt und dokumentiert, ein Dokument (Daten) speichert, aktualisiert, entfernt, abruft und anzeigt.
Verwendete Tools und Technologien:
MongoDB 2.2.3
MongoDB-Java-Driver 2.10.1
JDK 1.6
Maven 3.0.3
Eclipse 4.2
P.S Maven and Eclipse are both optional, just my personal favorite development tool.
Stellen Sie eine Verbindung zum MongoDB-Server her. Verwendet für die MongoDB-Version> = 2.10.0MongoClient.
// Old version, uses Mongo
Mongo mongo = new Mongo("localhost", 27017);
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient( "localhost" , 27017 );
Wenn sich MongoDB im sicheren Modus befindet, ist eine Authentifizierung erforderlich.
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("database name");
boolean auth = db.authenticate("username", "password".toCharArray());
4. Mongo-Datenbank
Datenbank abrufen. Wenn die Datenbank nicht vorhanden ist, erstellt MongoDB sie für Sie.
DB db = mongo.getDB("database name");
Alle Datenbanken anzeigen.
List dbs = mongo.getDatabaseNames();
for(String db : dbs){
System.out.println(db);
}
5. Mongo-Sammlung
Sammlung / Tisch holen.
DB db = mongo.getDB("testdb");
DBCollection table = db.getCollection("user");
Alle Sammlungen der ausgewählten Datenbank anzeigen.
DB db = mongo.getDB("testdb");
Set tables = db.getCollectionNames();
for(String coll : tables){
System.out.println(coll);
}
Note In RDBMS entspricht die Sammlung der Tabelle.
6. Beispiel speichern
Speichern Sie ein Dokument (Daten) in einer Sammlung (Tabelle) mit dem Namen "Benutzer".
DBCollection table = db.getCollection("user");
BasicDBObject document = new BasicDBObject();
document.put("name", "example");
document.put("age", 30);
document.put("createdDate", new Date());
table.insert(document);
Sehen Sie sich ein vollständiges Java + MongoDB-Beispiel an. Die Kommentare sind selbsterklärend.
App.java
package com.example.core;
import java.net.UnknownHostException;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
/**
* Java + MongoDB Hello world Example
*
*/
public class App {
public static void main(String[] args) {
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("testdb");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("user");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("name", "example");
document.put("age", 30);
document.put("createdDate", new Date());
table.insert(document);
/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "example");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/**** Update ****/
// search document where name="example" and update it with new values
BasicDBObject query = new BasicDBObject();
query.put("name", "example");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "example-updated");
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
table.update(query, updateObj);
/**** Find and display ****/
BasicDBObject searchQuery2
= new BasicDBObject().append("name", "example-updated");
DBCursor cursor2 = table.find(searchQuery2);
while (cursor2.hasNext()) {
System.out.println(cursor2.next());
}
/**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}