欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MongoDB demo

程序员文章站 2022-05-09 11:39:25
...

增删改查

package mytest.demo.mongo;

import java.net.UnknownHostException;
import java.util.List;

import mytest.demo.mongo.utils.MongoDbUtils;

import org.bson.types.ObjectId;
import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class MongoDbApi {
	
	
	@Test
	public void test() throws UnknownHostException {
		Mongo mongo = MongoDbUtils.getMongo("192.168.6.141", 27017);//new Mongo("localhost:27017");
		List<String> dbName = mongo.getDatabaseNames();

		for(String name:dbName){

			System.out.println("dbName:"+name);

		}
	}
	
	@Test
	public void add() throws UnknownHostException{
		
		//建立起连接...
		/*Mongo mongo=new Mongo("localhost:27017");
		DB db=mongo.getDB("demo");
		DBCollection collection=db.getCollection("employee");*/
		DBCollection collection = MongoDbUtils.getDBCollection("192.168.6.141", 27017,"demo", "employee");
		
		DBObject dbObject =new BasicDBObject ();
		
		dbObject.put("name", "李若亮2");
		dbObject.put("age", 17);
		dbObject.put("gender", "M");
		
		collection.insert(dbObject);
		MongoDbUtils.closeMongo();//mongo.close();
		
	}

	@Test
	public void update() throws UnknownHostException{
		//建立起连接...
/*		Mongo mongo=new Mongo("localhost", 27017);
		
		DB db=mongo.getDB("demo");
		DBCollection collection=db.getCollection("employee");*/
		DBCollection collection = MongoDbUtils.getDBCollection("192.168.6.141", 27017,"demo", "employee");
		
		/**
		 * 1:用于设置更新的条件
		 * 2:用于设置更新的内容的对象
		 * 3:如果没有符合条件的记录,是否新增一条。。 1true/flase
		 * 4:如果有多条记录符合,是否全部更新...
		 * 
		 * 
		 */
		//查找到object id =55e012179d4f1f67f3e21f10
		BasicDBObject queryObj = new BasicDBObject("_id",new ObjectId("55e012179d4f1f67f3e21f10"));
		
		BasicDBObject object = (BasicDBObject) collection.findOne(queryObj);
		
		object.put("name", "王小丫2");
		object.put("age", 20);
		object.put("gender", "F");
		
		//update objectId=55e012179d4f1f67f3e21f10  set Value=
		
		collection.update(queryObj,object);
		
		
		MongoDbUtils.closeMongo();//mongo.close();
	}
	
	@Test
	public void find() throws UnknownHostException{
		//建立起连接...
/*		Mongo mongo=new Mongo("localhost", 27017);
		
		DB db=mongo.getDB("demo");
		DBCollection collection=db.getCollection("employee");*/
		DBCollection collection = MongoDbUtils.getDBCollection("192.168.6.141", 27017,"demo", "employee");
		
		//返回结果集
		DBCursor cursor=collection.find();
		while(cursor.hasNext()){
			DBObject dbObject=cursor.next();
			System.out.println(dbObject.toString());
		}
		MongoDbUtils.closeMongo();//mongo.close();
	}
	
	
	@Test
	public void delete() throws UnknownHostException{
		//建立起连接...
/*		Mongo mongo=new Mongo("localhost", 27017);
		
		DB db=mongo.getDB("demo");
		DBCollection collection=db.getCollection("employee");	*/
		DBCollection collection = MongoDbUtils.getDBCollection("192.168.6.141", 27017,"demo", "employee");
		
		BasicDBObject basicDBObject=new BasicDBObject();
		//{}
		basicDBObject.put("name", "李若亮2");
		//{name:"李若亮"}
		
		collection.remove(basicDBObject);
		MongoDbUtils.closeMongo();//mongo.close();
	}
}

 

自定义工具类

package mytest.demo.mongo.utils;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;

public class MongoDbUtils {

	private static Mongo mongo = null;
	
	public static Mongo getMongo(String url,int port) throws UnknownHostException {
		mongo = new Mongo(url + ":" + port);
		return mongo;
	}
	
	public static DBCollection getDBCollection(String url,int port,String dbName,String collectionName) throws UnknownHostException {
		MongoDbUtils.getMongo(url, port);
		DB db = mongo.getDB(dbName);
		return db.getCollection(collectionName);
	}
	
	public static void closeMongo() {
		mongo.close();
	}
}

 

相关标签: mongo nosql