Mongodb 3.0.2 增、删、改、查 简单示例

 

使用 junit 4 测试 Mongodb3 增、删、改、查

 

数据库: Mongodb 3.0.2

jar包 : mongo-java-driver-3.0.2.jar

             junit-4.10.jar

数据库客户端工具: Robomongo

 

3.0.2 版本中,使用 MongoCollection<Document> 代替了原来的 DBCollection,

                       使用 MongoDatabase 代替了原来的 DB

 

java代码如下:

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

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

/**
 * @ClassName: TestMongodb
 * @deprecated: Mongodb3.0.2方法测试(junit4)
 * @author 
 * @company 
 * @date 2015-6-1
 * @version V1.0
 */

public class TestMongodb {

	private MongoCollection<Document> coll = null;

	@Before
	public void setUp() throws Exception {

		try {
			MongoClient mongoClient = new MongoClient("10.0.0.117", 27017);
			// 连接数据库,若没有,则新建
			MongoDatabase db = mongoClient.getDatabase("test");
			// 连接集合(类似于RDBMS)中的表,若没有,则新建
			coll = db.getCollection("testCollection");

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Test
	public void insert() {

		try {
			// 老版本
			// BasicDBObject doc = new BasicDBObject("name", "MongoDB")
			// .append("type", "database").append("count", 1)
			// .append("info", new BasicDBObject("x", 203).append("y", 102));
			// coll.insert(doc);

			/* 3.0.2版本 */
			Document doc = new Document();

			doc.put("cust_id", "A111");
			doc.put("amount", 210);
			doc.put("status", "C");

			coll.insertOne(doc);

			System.out.println("Insert Success!");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void query() {

		MongoCursor<Document> cursor = null;

		try {
			BasicDBObject query = new BasicDBObject("cust_id", "A111");

			FindIterable<Document> iter = coll.find(query);

			cursor = iter.iterator();

			System.out.println(cursor.hasNext());

			Document doc = null;

			while (cursor.hasNext()) {

				doc = cursor.next();

				System.out.println(doc.get("cust_id"));

				System.out.println(doc.toString());
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != cursor) {
				cursor.close();
			}
		}
	}

	@Test
	public void delete() {

		// BasicDBObject query = new BasicDBObject("status", "C");
		//
		// coll.deleteMany(query);

		// 根据主键(_id)删除
		BasicDBObject query = new BasicDBObject("_id", new ObjectId(
				"556d7121d49c6f19e0c76c8e"));

		coll.deleteOne(query);

		// 查找集合(表)中所有的文档(行)
		FindIterable<Document> iterable = coll.find();

		MongoCursor<Document> cursor = iterable.iterator();

		Document doc = null;

		while (cursor.hasNext()) {
			doc = cursor.next();
			System.out.println(doc.toString());
		}
	}

	@Test
	public void update() {
		try {
			// 查找_id=556d7121d49c6f19e0c76c8e的文档(行)
			BasicDBObject searchQuery = new BasicDBObject().append("_id",
					new ObjectId("556d7121d49c6f19e0c76c8e"));

			System.out.println(searchQuery);

			BasicDBObject newDocument = new BasicDBObject();
			// 将其中的Key为cust_id的值改为ABC123
			newDocument.append("$set",
					new BasicDBObject().append("cust_id", "ABC123"));

			coll.updateOne(searchQuery, newDocument);

			System.out.println("Update Success!");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(Mongodb 3.0.2 增、删、改、查 简单示例)