MongoDB: Database, collection, document, where "set" is the corresponding relational database "table", "document" corresponding to the "line".
1, first of all, download the MongoDB Java-supported driver package
<dependency>
<groupId> org.mongodb</groupid >
<artifactId> mongo-java-driver </ artifactid>
<version> 2.9.3</version>
</dependency>
2. Java Connection MongoDB Database
/**
URL Database IP Address
Port ports
Dbnname database name
Tmcinfo Collection Name
**/
Mongo Mongo = new Mongo (Url,integer.parseint (port));
db = Mongo.getdb (dbname);
if (db.authenticate (user, Pwd.tochararray ()) {return
true;
}
Dbcollection users = db.getcollection ("Tmcinfo");
3. CRUD operation
Query:
Dbcursor cur = users.find ();
while (Cur.hasnext ()) {
System.out.println (Cur.next ());
}
Query deletion: Query for age=25 data and delete
Dbcollection.findandremove (New Basicdbobject ("Age", 25))
Insert:
Basicdbobject document = new Basicdbobject ();
Document.put ("id", 1001);
Document.put ("msg", "Hello World MongoDB in Java");
Dbcollection dbcollection = db.getcollection (table_name);
Dbcollection. Insert (document);
BULK INSERT: A BULK insert of a single TCP request, which avoids the overhead of a fragmented request.
list<dbobject> list = new arraylist<dbobject> ();
Dbcollection. Insert (list). GETN ());//getn () Affect number of rows
Delete:
Dbcollection. Remove (New Basicdbobject ("Age", New Basicdbobject ("$gte")). GETN ())
Deletes the entire collection dbcollection.remove (); Slower than db.drop_collection (table_name), which deletes the collection directly, but all indexes are also deleted
Modify:
Dbcollection. Update (New Basicdbobject ("_id", New ObjectId ("4dde2b06feb038463ff09042")), New Basicdbobject ("Age", 121 )
Returns the updated document query age=26 data, and modifies the value of name to ABC
Dbcollection.findandmodify (New Basicdbobject ("Age,"), New Basicdbobject ("name", "ABC"));
Java-mongodb
Address of API documentation
http://api.mongodb.org/java/
Official Entry Address Http://www.mongodb.org/display/DOCS/Java+Tutorial