(JAVA)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();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值