1. Download the driver https://github.com/mongodb/developer-java-driver/downloadsand import it to Java.
2. Create a test code
Import java.net. unknownhostexception;
Import java. util. Set;
Import com. MongoDB. basicdbobject;
Import com. MongoDB. dB;
Import com. MongoDB. dbcollection;
Import com. MongoDB. dbcursor;
Import com. MongoDB. dbobject;
Import com. MongoDB. Mongo;
Import com. MongoDB. parse exception;
Public class testmain {
Public static void main (string [] ARGs) throws unknownhostexception, except exception {
// Mongo M = new Mongo (); // default local
// Mongo M = new Mongo ("192.168.0.101"); // default port
Mongo M = new Mongo ("192.168.0.101", 27017 );
// Obtain the database named Alan. It is created if it does not exist.
DB = M. getdb ("Alan ");
// Retrieve all databases. databases without collection are not displayed.
System. Out. println ("All Database names:" + M. getdatabasenames ());
// Obtain the collection named testcollection (equivalent to a table), which is created if it does not exist.
Dbcollection coll = dB. getcollection ("testcollection ");
// Insert a value to the Collection object (you can insert a value)
Basicdbobject OBJ = new basicdbobject ();
OBJ. Put ("name", "Jone ");
OBJ. Put ("sex", "male ");
Basicdbobject info = new basicdbobject ();
Info. Put ("height", 172 );
Info. Put ("weight", 65 );
OBJ. Put ("other", Info );
Coll. insert (OBJ );
// Obtain all collections in the database. No collection with no data is displayed.
Set <string> colls = dB. getcollectionnames ();
For (string S: colls ){
System. Out. println (s );
}
// Query all records in Coll
Dbcursor ite = Coll. Find ();
While (ITE. hasnext ()){
System. Out. println (ITE. Next ());
}
// Obtain the first record
Dbobject o = Coll. findone ();
System. Out. println (O );
// Count the number of colletion data items
System. Out. println (Coll. getcount ());
// Query the object of the name Mark
Basicdbobject query = new basicdbobject ();
Query. Put ("name", "Mark ");
Dbcursor it = Coll. Find (query );
While (it. hasnext ()){
System. Out. println (it. Next ());
}
// Query objects whose height is less than 175 and weight is not 65
Basicdbobject query2 = new basicdbobject ();
Query2.put ("other. Height", new basicdbobject ("$ lt", 175 ));
Query2.put ("other. Weight", new basicdbobject ("$ ne", 65 ));
Dbcursor it2 = Coll. Find (query2 );
While (it2.hasnext ()){
System. Out. println (it2.next ());
}
// Update operation
Showdata (Coll );
Basicdbobject old_obj = new basicdbobject ();
Old_obj.put ("name", "Mark ");
// The new_val object must be found instead of New. Otherwise, other field information will be lost when multiple fields exist.
Dbobject new_val = Coll. findone (old_obj );
New_val.put ("name", "zhoulong ");
/** Only one record meeting the condition can be modified here, And the updatemulti method or the fourth update parameter setting is also invalid according to the API,
* If you want to query data in batches and update data in batches, the system cyclically traverses and updates the data.
*/
Coll. Update (old_obj, new_val );
Showdata (Coll );
// Delete operation
Showdata (Coll );
Basicdbobject rmove = new basicdbobject ();
Rmove. Put ("name", "Jone ");
Coll. Remove (rmove );
// Coll. findandremove (rmove); // you can use findandremove to delete a qualified record.
Showdata (Coll );
}
// Traverse data
Static void showdata (dbcollection col)
{
Dbcursor ite = col. Find ();
While (ITE. hasnext ())
{
System. Out. println (ITE. Next ());
}
}
}
3, reference API, http://api.mongodb.org/java/2.5-pre-/index.html
4. Use a graphical interface to view the newly created database tables and inserted data.