mongodb学习javaAPI

一、驱动包 maven配置

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>


二、java api操作

 25     public void testCRUD() throws UnknownHostException {
 26         // 连接到mongodb
 27         Mongo mongo = new Mongo("localhost", 27017);
 28 
 29         // 打开数据库test
 30         DB db = mongo.getDB("test");
 31 
 32         // 遍历所有集合的名字
 33         Set<String> colls = db.getCollectionNames();
 34         for (String s : colls) {
 35             System.out.println(s);
 36             // 先删除所有Collection(类似于关系数据库中的"表")
 37             if (!s.equals("system.indexes")) {
 38                 db.getCollection(s).drop();
 39             }
 40         }
 41 
 42         // 取得集合emp(若:emp不存在,mongodb将自动创建该集合)
 43         DBCollection coll = db.getCollection("emp");
 44 
 45         // delete all
 46         DBCursor dbCursor = coll.find();
 47         for (DBObject dbObject : dbCursor) {
 48             coll.remove(dbObject);
 49         }
 50 
 51         // create
 52         BasicDBObject doc = new BasicDBObject("name", "杨俊明").append("sex", "男")
 53                 .append("address",
 54                         new BasicDBObject("postcode", "201202").append(
 55                                 "street", "田林路888号").append("city", "上海"));
 56         coll.insert(doc);
 57 
 58         // retrieve
 59         BasicDBObject docFind = new BasicDBObject("name", "杨俊明");
 60         DBObject findResult = coll.findOne(docFind);
 61         System.out.println(findResult);
 62 
 63         // update
 64         doc.put("sex", "MALE");// 把sex属性从"男",改成"MALE"
 65         coll.update(docFind, doc);
 66         findResult = coll.findOne(docFind);
 67         System.out.println(findResult);
 68 
 69         coll.dropIndexes();// 先删除所有索引
 70         // create index
 71         coll.createIndex(new BasicDBObject("name", 1)); // 1代表升序
 72 
 73         // 复杂对象
 74         UserData userData = new UserData("jimmy", "123456");
 75         Set<String> pets = new HashSet<String>();
 76         pets.add("cat");
 77         pets.add("dog");
 78         Map<String, String> favoriteMovies = new HashMap<String, String>();
 79         favoriteMovies.put("dragons", "Dragons II");
 80         favoriteMovies.put("avator", "Avator I");
 81         userData.setFavoriteMovies(favoriteMovies);
 82         userData.setPets(pets);
 83         userData.setBirthday(getDate(1990, 5, 1));
 84         BasicDBObject objUser = new BasicDBObject("key", "jimmy").append(
 85                 "value", toDBObject(userData));
 86         coll.insert(objUser);
 87         System.out.println(coll.findOne(objUser));
 88     }
 89 
 90     /**
 91      * 将普通Object对象转换成mongodb的DBObject对象
 92      * 
 93      * @param obj
 94      * @return
 95      */
 96     private DBObject toDBObject(Object obj) {
 97         Gson gson = new Gson();
 98         String json = gson.toJson(obj);
 99         return (DBObject) JSON.parse(json);
100     }
101 
102     /**
103      * 获取指定日期
104      * 
105      * @param year
106      * @param month
107      * @param day
108      * @return
109      */
110     private Date getDate(int year, int month, int day) {
111         Calendar calendar = Calendar.getInstance();
112         calendar.clear();
113         calendar.set(year, month - 1, day);
114         return calendar.getTime();
115 
116     }
117 
118 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值