设为首页 加入收藏

TOP

MongoDB之Java测试代码(DAO层)(三)
2015-11-21 01:52:42 来源: 作者: 【 】 浏览:10
Tags:MongoDB Java 测试 代码 DAO
bject(); query22.put("d", 6); List orQueryList2 = new ArrayList(); orQueryList2.add(query21); orQueryList2.add(query22); BasicDBObject orQuery2 = new BasicDBObject("$or", orQueryList2); List orQueryCombinationList = new ArrayList(); orQueryCombinationList.add(orQuery1); orQueryCombinationList.add(orQuery2); BasicDBObject finalQuery = new BasicDBObject("$and", orQueryCombinationList); DBCursor cursor = MongoInit.getColl("wujintao").find(finalQuery); } /** * IN查询 * if i need to query name in (a,b); just use { name : { $in : ['a', 'b'] } } * select * from things where name='a' or name='b' * @param coll */ public void queryIn() { BasicDBList values = new BasicDBList(); values.add("a"); values.add("b"); BasicDBObject in = new BasicDBObject("$in", values); DBCursor cursor = MongoInit.getColl("wujintao").find( new BasicDBObject("name", in)); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } } /** * 或查询 * select * from table where name = '12' or title = 'p' * @param coll */ public void queryOr() { QueryBuilder query = new QueryBuilder(); query.or(new BasicDBObject("name", 12), new BasicDBObject("title", "p")); DBCursor cursor = MongoInit.getColl("wujintao").find(query.get()).addSpecial("$returnKey", ""); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } } public void customQueryField() throws UnknownHostException{ Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("zhongsou_ad"); BasicDBObjectBuilder bulder = new BasicDBObjectBuilder(); bulder.add("times",1); bulder.add("aid",1); DBCursor cusor = db.getCollection("ad_union_ad_c_1").find(new BasicDBObject(),bulder.get()); for (DBObject dbObject : cusor) { System.out.println(dbObject); } } public void mapReduce() throws UnknownHostException{ Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("zhongsou_ad"); /*** * book1 = {name : "Understanding JAVA", pages : 100} * book2 = {name : "Understanding JSON", pages : 200} * db.books.save(book1) * db.books.save(book2) * book = {name : "Understanding XML", pages : 300} * db.books.save(book) * book = {name : "Understanding Web Services", pages : 400} * db.books.save(book) * book = {name : "Understanding Axis2", pages : 150} * db.books.save(book) * var map = function() { var category; if ( this.pages >= 250 ) category = 'Big Books'; else category = "Small Books"; emit(category, {name: this.name}); }; var reduce = function(key, values) { var sum = 0; values.forEach(function(doc) { sum += 1; }); return {books: sum}; }; var count = db.books.mapReduce(map, reduce, {out: "book_results"}); */ try { DBCollection books = db.getCollection("books"); BasicDBObject book = new BasicDBObject(); book.put("name", "Understanding JAVA"); book.put("pages", 100); books.insert(book); book = new BasicDBObject(); book.put("name", "Understanding JSON"); book.put("pages", 200); books.insert(book); book = new BasicDBObject(); book.put("name", "Understanding XML"); book.put("pages", 300); books.insert(book); book = new BasicDBObject(); book.put("name", "Understanding Web Services"); book.
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mariadb简介 下一篇故障案例:slave延迟很大

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: