Java操作MongoDB

Java操作MongoDB

首先需要添加进入Java-MongoDB驱动包

java操作Mongodb常用的几个类:

Mongo:连接服务器,执行一些数据库操作的选项,如新建立一个数据库等

DB:对应一个数据库,可以用来建立集合等操作

DBCollection:对应一个集合(类似表),可能是我们用得最多的,可以添加删除记录等

DBObject接口和BasicDBObject对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key-value的数据结构,用起来和HashMap是基本一致的。

DBCursor:用来遍历取得的数据,实现了Iterable和Iterator

具体实现:

package m1;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

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.QueryOperators;

/**
 * Java对MongoDB的操作
 * @author linziyu
 *
 */
public class TT {
	Mongo mongo;
	public TT(Mongo mongo) {
		this.mongo = mongo;
		
	}
	
	/**
	 * 插入一条文档
	 */
	public void insert() {
		DB db = mongo.getDB("data1");
		DBCollection collection = db.getCollection("user");
		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put("name", "KKK");
		basicDBObject.put("age",15);
		basicDBObject.put("sex","man");
		collection.insert(basicDBObject);
	}
	
	public void insert_Many() {
		DB db = mongo.getDB("data1");
		DBCollection collection = db.getCollection("u1");
		List objects = new LinkedList<>();
		objects.add(new BasicDBObject("name","a").append("age",77).append("sex","woman"));
		objects.add(new BasicDBObject("name","b").append("age",99).append("sex","woman"));
		objects.add(new BasicDBObject("name","c").append("age",99).append("sex","woman"));
		objects.add(new BasicDBObject("name","d").append("age",99).append("sex","woman"));
		objects.add(new BasicDBObject("name","e").append("age",99).append("sex","woman"));
		collection.insert(objects);
	}
	
	/**
	 * 读取所有记录
	 */
	public void read_ALL() {
		DB db = mongo.getDB("data1");
		DBCollection collection = db.getCollection("u1");
		DBCursor cursor = collection.find();
		while (cursor.hasNext()) {
			DBObject object = cursor.next();
			System.out.println(object);
			
		}
		
	}
	
	/**
	 * 读取数据
	 * 
	 */
	public void read() {
		DB db = mongo.getDB("data1");
		DBCollection collection = db.getCollection("u1");
		DBCursor cursor = collection.find();
		while (cursor.hasNext()) {
			BasicDBObject basicDBObject = (BasicDBObject) cursor.next();
			if (basicDBObject!=null) {
				System.out.println(basicDBObject.getString("name"));
				System.out.println(basicDBObject.getInt("age"));
				System.out.println(basicDBObject.getString("sex"));
				
			}
			
		}
	}
	
	/**
	 * 条件查询(查找name值为ppp的对象)
	 */
	public void find() {
		DB db = mongo.getDB("data1");
		DBCollection collection = db.getCollection("user");
		BasicDBObject queryObject = new BasicDBObject("name","ppp");
		DBObject object = collection.findOne(queryObject);
		System.out.println(object.get("age"));
	}
	
	
	/**
	 * 条件查询(查找年龄大于23并且sex值为woman的对象)
	 */
	public void find_1() {
		 DB db = mongo.getDB("data1");
		 DBCollection collection = db.getCollection("user");
		 BasicDBObject ageObj = new BasicDBObject("age",new BasicDBObject("$gt",23));
	     BasicDBObject sexObj = new BasicDBObject("sex","woman");
	     BasicDBObject andObj = new BasicDBObject("$and",Arrays.asList(ageObj,sexObj));
	     DBCursor cursor = collection.find(andObj);
	     while(cursor.hasNext()){
	                DBObject obj = cursor.next();
	                System.out.println(obj.toString());

	        }
		
	}
	
	/**
	 * 更新数据(把表中字段age都改成55)
	 */
	public void update() {
		 DB db = mongo.getDB("data1");
		 DBCollection collection = db.getCollection("u1");
		 DBCursor cursor = collection.find();
		 BasicDBObject doc = new BasicDBObject();
		 BasicDBObject res = new BasicDBObject();
		 res.put("age",55);
		 doc.put("$set",res);
		 collection.update(new BasicDBObject(),doc,false,true);
		 
		 
	}
	
	/**
	 * 删除(删除name为a的记录)
	 */
	public void delete() {
		 DB db = mongo.getDB("data1");
		 DBCollection collection = db.getCollection("u1");
		 BasicDBObject object = new BasicDBObject();
		 object.put("name","a");
		 collection.remove(object);
		 
		
	}
	
	
}

你可能感兴趣的:(MongoDB学习)