MongoDB-Java adds, deletes, modifies and checks

MongoDB-Java adds, deletes, modifies and checks

  Summarize:

Operation summary:
1. Steps: Create an instance (Mongo)-->Get the database (DB)-->Get the collection (table) (DBCollection)-->Get the data in the collection (DBCursor)
        --> operate on the data
 2. No table concept, set to replace.

 Code case:

package mongo.util;

import java.net.UnknownHostException;
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.util.JSON;

public class MonUtil {
/**
 * Mongodb java operation CRUD
 * @param args
 * @throws UnknownHostException
 */

/**
 * Operation summary:
 * 1. Steps: Create an instance (Mongo)-->Get the database (DB)-->Get the collection (table) (DBCollection)-->Get the data in the collection (DBCursor)
 * --> operate on the data
 * 2. No table concept, set to replace.
 */
public static void main(String[] args) throws UnknownHostException {
	      //Create a Mongo instance, use the local "127.0.0.1" by default, port 27017
	      // Mongo mongo1 = new Mongo ();
              //Create an instance using IP+port number
	       Mongo mongo02 = new Mongo("127.0.0.1",27017);
		// get the names of all databases
		for(String name:mongo02.getDatabaseNames()){
			System.out.println("dbName: "+name);
		}
		
	    //Get a database for subsequent operations on the database
		DB db = mongo02.getDB("mymongo");
		
		// get all collections
		System.out.println("Loop through all collections");
		for(String name: db.getCollectionNames()){
			System.out.println("collectionName: "+name);
		}
		// get the corresponding set
		DBCollection users = db.getCollection("sys_user");
		
		// get the data in the collection
		DBCursor cur = users.find();
		// loop through all the contents in the collection
		System.out.println("Iterate over the contents of the collection: ");	
		while(cur.hasNext()){
		   System.out.println(cur.next());	
		}
		System.out.println("cur.count(): " + cur.count());
		System.out.println("cur.getCursorId(): " + cur.getCursorId());
		System.out.println("JSON.serialize(cur): " + JSON.serialize(cur));
		
		//delete
		users.remove(new BasicDBObject("name","wangshuai"));
		System.out.println("删除--cur.count(): " + cur.count());
		
		//add
		DBObject user1 = new BasicDBObject();
		user1.put("name", "dada");
		user1.put("age","28");
		user1.put("sex", "man");
		users.save(user1);
		System.out.println("新增--cur.count(): " + cur.count());
		
		//Revise
		DBObject user_update = new BasicDBObject();
		user_update.put("name", "wangwu");
		user_update.put("age","18");
		user_update.put("sex", "man");
		users.update(new BasicDBObject("name", "wangwu"),user_update);
		System.out.println("修改--"+JSON.serialize(users.find(new BasicDBObject("name", "wangwu"))));
		System.out.println("Loop through all data: ");
		System.out.println("JSON.serialize(cur): " + JSON.serialize(cur));
	}
}

   

    Console printout:

dbName: KM
dbName: local
dbName: mymongo
dbName: admin
iterate over all collections
collectionName: sys_user
collectionName: system.indexes
Iterate over the contents of the collection:
{ "_id" : { "$oid" : "5a7409448d2ed42d1280a7c1"} , "name" : "wangwu" , "age" : "18" , "sex" : "man"}
{ "_id" : { "$oid" : "5a74098e8d2e75e7dfc7adbc"} , "name" : "王琦" , "age" : "18" , "sex" : "man"}
{ "_id" : { "$oid" : "5a740a528d2ebd5bc064801f"} , "name" : "chenchen" , "age" : "18" , "sex" : "man"}
{ "_id" : { "$oid" : "5a740af78d2e926c7de492cc"} , "name" : "xuxu" , "age" : "28" , "sex" : "man"}
{ "_id" : { "$oid" : "5a740b2d8d2ede3a44c78876"} , "name" : "niannian" , "age" : "28" , "sex" : "man"}
cur.count(): 5
cur.getCursorId(): 0
JSON.serialize(cur): [ { "_id" : { "$oid" : "5a7409448d2ed42d1280a7c1"} , "name" : "wangwu" , "age" : "18" , "sex" : "man"} , { "_id" : { "$oid" : "5a74098e8d2e75e7dfc7adbc"} , "name" : "王琦" , "age" : "18" , "sex" : "man"} , { "_id" : { "$oid" : "5a740a528d2ebd5bc064801f"} , "name" : "chenchen" , "age" : "18" , "sex" : "man"} , { "_id" : { "$oid" : "5a740af78d2e926c7de492cc"} , "name" : "xuxu" , "age" : "28" , "sex" : "man"} , { "_id" : { "$oid" : "5a740b2d8d2ede3a44c78876"} , "name" : "niannian" , "age" : "28" , "sex" : "man"}]
remove --cur.count(): 5
Added --cur.count(): 6
修改--[ { "_id" : { "$oid" : "5a7409448d2ed42d1280a7c1"} , "name" : "wangwu" , "age" : "18" , "sex" : "man"}]
Iterate over all data:
JSON.serialize(cur): [ { "_id" : { "$oid" : "5a7409448d2ed42d1280a7c1"} , "name" : "wangwu" , "age" : "18" , "sex" : "man"} , { "_id" : { "$oid" : "5a74098e8d2e75e7dfc7adbc"} , "name" : "王琦" , "age" : "18" , "sex" : "man"} , { "_id" : { "$oid" : "5a740a528d2ebd5bc064801f"} , "name" : "chenchen" , "age" : "18" , "sex" : "man"} , { "_id" : { "$oid" : "5a740af78d2e926c7de492cc"} , "name" : "xuxu" , "age" : "28" , "sex" : "man"} , { "_id" : { "$oid" : "5a740b2d8d2ede3a44c78876"} , "name" : "niannian" , "age" : "28" , "sex" : "man"} , { "_id" : { "$oid" : "5a740b778d2e4d08297fc74e"} , "name" : "dada" , "age" : "28" , "sex" : "man"}]

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326048396&siteId=291194637