1. Download the driver https://github.com/mongodb/mongo-java-driver/downloads and import it into the project java
2, build 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.MongoException;
public class TestMain {
public static void main(String[] args) throws
UnknownHostException, MongoException {
// 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);
//Get the database named alan, create it if it does not exist
DB db = m. getDB(“alan”);
//Get all databases, do not display db without collection
System.out.println(“All database names: “+m.getDatabaseNames());
//Get the collection named testCollection (equivalent to a table), and create it if it does not exist
DBCollection coll =
db.getCollection(“testCollection”);
//Insert value into collection (insert can be inserted)
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);
//Get all collections under the database, do not display collections without data
Set 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());
}
//Get the first record
DBObject o = coll. findOne();
System.out.println(o);
“
//Statistics of the number of colletion data
System.out.println(coll.getCount());
// Query the name bit
object of mark
BasicDBObject
query = new BasicDBObject();
query. put(“name”,
“mark”);
DBCursor it =
coll.find(query);
while(it.hasNext()){
System.out.println(it.next());
}
//Query the objects whose height is less than 175 and weight is not equal to 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 here must be found instead of new, otherwise other field information will be lost in the case of multiple fields
DBObject new_val =
coll.findOne(old_obj);
new_val.put(“name”, “zhoulong”);
/**Only one record that satisfies the conditions can be modified here, and it is invalid to use the updateMulti method or set the fourth parameter of update according to the API,
* If you want to update in batches, you need to query and loop through the updates
*/
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);//Can be deleted with findAndRemove
, but this method can only delete a record that meets the conditions
showData(coll);
}
//Loop through the data
static void showData(DBCollection col)
{
DBCursor ite = col. find();
while(ite.hasNext())
{
System.out.println(ite.next());
}
}
}
3. Refer to api, http://api.mongodb.org/java/2.5-pre-/index.html
4. Use the graphical interface to visually view the newly created database tables and inserted data