java basic operation of mongodb

mongodb is liked by many programmers because it stores a lot of data, supports high concurrency and weak transactions.

For java, you need to download the java mongo driver package, similar to the mysql driver package, which contains the java operation mongo tool class.

DBCollection is used for driver package versions below 3.0...
The MongoCollection Document is used when the driver package version is above 3.0...

I am accustomed to using version 3.0 below, as follows:

DBCollection class: the collection in the index database, which provides the basic method to obtain the collection table. In the relational database, it is a table, while in the non relational database, it is a collection;

DBObject class: a document in a collection. Generally speaking, a record value in a database table;

DBCursor class: refers to the collection of documents returned by the cursor.


Connection service

        //Connect to mongodb service
        Mongo mongo=new MongoClient("",20000);
        //Connect to database
        DB db=mongo.getDB("myMongoDB");
        //Operate on the course set in the myMongoDB database 
        DBCollection course = db.getCollection("course");


There are four ways to insert a single piece of data:
1. DBObject. Append() 2. DBObject. Put() 3. Convert json to DBObject object object through map 4
Insert batch data usage list

//The first kind of append
DBObject english = new BasicDBObject().append("name","english").append("score", 5).append("id",1);  
//Second put
DBObject math = new BasicDBObject();  
math.put("id", 2);  
math.put("name", "math");  
math.put("score", 10);  
//The third kind of map    
Map<String,Object> map = new HashMap<String,Object>();  
map.put("name","physics" );  
map.put("score", 10);  
map.put("id", 3);  
DBObject physics= new BasicDBObject(map);  
//The fourth kind of json      
String json ="{'name':'chemistry','score':10,'id':4}";  
DBObject chemistry =(DBObject)JSON.parse(json);  
//Fifth batch insert multiple records
List<DBObject> courseList = new ArrayList<DBObject>();  
DBObject chinese = new BasicDBObject().append("name","chinese").append("score", 10).append("id", 5);  
DBObject history = new BasicDBObject().append("name", "history").append("score", 10).append("id", 6);  
//Add embedded document  
String json2 =" {'name':'english','score':10,'teacher':[{'name':'Liu Song','id':'1'},{'name':'Willow pine','id':2}]}";  
DBObject english2= (DBObject)JSON.parse(json);  

List<DBObject> list = new ArrayList<DBObject>();  
list.add(new BasicDBObject("name","Liu Song").append("id",1));  
list.add(new BasicDBObject("name","Willow pine").append("id",2));  
DBObject english3= new BasicDBObject().append("name","english").append("score",10).append("teacher",list); 


Only basic queries are listed here, such as single query and condition query. For advanced aggregate query statistics, see the next blog

//Inquiry office  
DBCursor cur = course.find();  
      DBObject document =;  
//Query a document       
DBObject document = course.findOne();  
String name=(String)document.get("name");  

//Conditional query for credit = 5  
DBObject query1 = new BasicDBObject("score",5);  
//Find those with credits greater than 5
DBObject query2 = new BasicDBObject("score",new BasicDBObject("$gte",5));  
DBCursor cur2 = course.find(query2);  

//Conditional expression: $Ge (>) $get (> =) $LT (<) $LTE (< =) $ne (< >) $in $Nin $all $exists $or $nor $where $type, etc  

//Find and modify       
DBObject newDocument = course.findAndModify(new BasicDBObject("score",5), new BasicDBObject("score",15));  


//q: Update condition o: updated object  
course.update(new BasicDBObject("score",10), new BasicDBObject("test",15));  
course.update(new BasicDBObject("score",15), new BasicDBObject("$set",new BasicDBObject("isRequired",true)));  
//The difference between the two is that the first update will{"test":15}This document replaces the original document,  
//The second update adds a conditional expression$set,It is added on the basis of the original document"isRequired"This key  

//Conditional expression:$set  $unset  $push  $inc  $push $push  $addToSet  $pull $pullAll  $popWait  

//When the ﹣ id is the same, executing the save method is equivalent to an update operation BasicDBObject("name","math").append("_id", 1)); BasicDBObject("name","Mathematics").append("_id", 1));  


//Delete eligible documents  
course.remove(new BasicDBObject("score",15));  

//Delete collection and all documents  

Tags: JSON Database Java MongoDB

Posted on Thu, 30 Apr 2020 01:52:10 -0700 by jdm95lude