package com.panshao.test; import java.util.ArrayList; import java.util.List; import org.bson.types.ObjectId; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; public class CRUDtest { private Mongo mg = null; private DB db; private DBCollection users; @Before public void init(){ try{ mg = new Mongo(); }catch(Exception e){ e.printStackTrace(); } db = mg.getDB("test"); users = db.getCollection("users"); } @After public void destory(){ print("------------------------"); if(mg != null){ mg.close(); } mg = null; db = null; users = null; System.gc(); } public void print(Object o){ System.out.println(o); } /** * 查询users的所有数据 */ private void queryAll(){ print("查询users的所有数据:"); DBCursor cur = users.find(); while(cur.hasNext()){ print(cur.next()); } } /** *这个例子虽然我修改了一些,但是感觉还是有点问题,再看看之后再来修改 * 1,为什么不能复用对象? * 每次都要重新new 出一个新的对象,不然后面执行的插入操作都不成功 * DBObject user = new BasicDBObject(); * * 2,为什么每次getN()都是0,这个玩意是什么意思来的? */ @Test public void add(){ queryAll(); print("count: "+users.count());//2 DBObject user = new BasicDBObject(); user.put("name", "panshao"); user.put("age", 24); //扩展字段,随意添加字段,不影响现有数据 user.put("sex", "男"); print("users.save(pansaho): "+users.save(user).getN());//3 //添加多条数据,传递Array对象 DBObject user2 = new BasicDBObject(); user2.put("name", "petty"); user2.put("age", 3); user2.put("height", 1.80); print("users.save(tom): "+users.insert(user2,new BasicDBObject("name", "tom")).getN());//5 List<DBObject> list = new ArrayList<DBObject>(); DBObject user3 = new BasicDBObject("name","lily"); user3.put("age", 22); list.add(user3); DBObject user4 = new BasicDBObject("name","lucy"); user4.put("age", 22); list.add(user4); print("users.save(panshao,lucy): "+users.insert(list).getN());//7 print("count: "+users.count()); queryAll(); } @Test public void remove() { queryAll(); print("删除id = 51ff519e7027f3055f892eb2:" + users.remove(new BasicDBObject("_id", new ObjectId("51ff519e7027f3055f892eb2"))).getN()); print("remove age < 5: " + users.remove(new BasicDBObject("age", new BasicDBObject("$lt", 5))).getN()); print("remove age = 22: " + users.remove(new BasicDBObject("age", 22)).getN()); queryAll(); } @Test public void modify() { queryAll(); //为什么在修改一个属性的时候会把原来整条记录的其它字段去除掉了,除了ID不变外,感觉就是一条全新的记录。 /** * update(BasicDBobject ,BasicDBobject) 第一个参数是查找条件,需要修改的对象,第二个参数是修改内容,如果不用set就是把原来的对象更新为现在的对象。 如果有$set那就是更新属性,如果属性不存在则添加。其他参数使用方法一样。 */ print("修改:" + users.update(new BasicDBObject("_id", new ObjectId("51ff5da77027fac30bd9a218")), new BasicDBObject("$set", new BasicDBObject("age",34))).getN()); queryAll(); print("修改:" + users.update( new BasicDBObject("_id", new ObjectId("4dde2b06feb038463ff09042")), new BasicDBObject("age", 121), true,//如果数据库不存在,是否添加,如果存在的话也会被直接被一个全新的对象取代掉了,age以后的属性都会被清除掉了 false ).getN()); queryAll(); BasicDBObject user = new BasicDBObject("name","jack"); user.put("age", 0000); user.put("sex", "male"); print("修改:" + users.update( new BasicDBObject("name", "dd"), new BasicDBObject("$set", user), true,//如果数据库不存在,是否添加 false//false只修改第一条,true如果就修改多条 ).getN()); queryAll(); } @Test public void querys(){ queryAll(); } /** * 请先执行这个函数 * db.system.js.save({_id:"insertList",value:function(list){for(var i=0; i < list.length; i++){ db.users.save(list[i]);}}}); */ @Test public void func(){ queryAll(); //insertList是存储在db.system.js中的函数,接受一个数组参数 String proc="function(list){ return insertList(list);}"; //Java 的List可以对应到JS的数组 List<DBObject> list = new ArrayList<DBObject>(); DBObject user1 = new BasicDBObject(); user1.put("name", "mike"); DBObject user2 = new BasicDBObject(); user2.put("name", "jason"); list.add(user1); list.add(user2); try { CommandResult cr = db.doEval(proc, list); print("result:"+cr.toString()); } catch (MongoException e) { e.printStackTrace(); } queryAll(); } }