MongoDB基本用法(增删改高级查询、mapreduce)
TestCase.java
- package com.wujintao.mongo;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- import java.util.regex.Pattern;
- import org.junit.Test;
- import com.mongodb.AggregationOutput;
- import com.mongodb.BasicDBList;
- import com.mongodb.BasicDBObject;
- import com.mongodb.BasicDBObjectBuilder;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.MapReduceCommand;
- import com.mongodb.MapReduceOutput;
- import com.mongodb.Mongo;
- import com.mongodb.QueryBuilder;
- import com.mongodb.WriteConcern;
- public class TestCase {
- //DBCursor cursor = coll.find(condition).addOption(Bytes.QUERYOPTION_NOTIMEOUT);//设置游标不要超时
- @Test
- /**
- * 获取所有数据库实例
- */
- public void testGetDBS() {
- List<String> dbnames = MongoUtil.getMong().getDatabaseNames();
- for (String dbname : dbnames) {
- System.out.println("dbname:" + dbname);
- }
- }
- @Test
- /**
- * 删除数据库
- */
- public void dropDatabase() {
- MongoUtil.getMong().dropDatabase("my_new_db");
- }
- @Test
- /**
- * 查询所有表名
- */
- public void getAllCollections() {
- Set<String> colls = MongoUtil.getDB().getCollectionNames();
- for (String s : colls) {
- System.out.println(s);
- }
- }
- @Test
- public void dropCollection() {
- MongoUtil.getColl("jellonwu").drop();
- }
- /**
- * 添加一条记录
- */
- @Test
- public void addData() {
- DBCollection coll = MongoUtil.getColl("wujintao");
- BasicDBObject doc = new BasicDBObject();
- doc.put("name", "MongoDB");
- doc.put("type", "database");
- doc.put("count", 1);
- BasicDBObject info = new BasicDBObject();
- info.put("x", 203);
- info.put("y", 102);
- doc.put("info", info);
- coll.insert(doc);
- // 设定write concern,以便操作失败时得到提示
- coll.setWriteConcern(WriteConcern.SAFE);
- }
- @Test
- /**
- * 创建索引
- */
- public void createIndex() {
- MongoUtil.getColl("wujintao").createIndex(new BasicDBObject("i", 1));
- }
- @Test
- /**
- * 获取索引信息
- */
- public void getIndexInfo() {
- List<DBObject> list = MongoUtil.getColl("hems_online").getIndexInfo();
- for (DBObject o : list) {
- System.out.println(o);
- }
- }
- @Test
- /**
- * 添加多条记录
- */
- public void addMultiData() {
- for (int i = 0; i < 100; i++) {
- MongoUtil.getColl("wujintao").insert(
- new BasicDBObject().append("i", i));
- }
- List<DBObject> docs = new ArrayList<DBObject>();
- for (int i = 0; i < 50; i++) {
- docs.add(new BasicDBObject().append("i", i));
- }
- MongoUtil.getColl("wujintao").insert(docs);
- // 设定write concern,以便操作失败时得到提示
- MongoUtil.getColl("wujintao").setWriteConcern(WriteConcern.SAFE);
- }
- @Test
- /**
- * 查找第一条记录
- */
- public void findOne() {
- DBObject myDoc = MongoUtil.getColl("wujintao").findOne();
- System.out.println(myDoc);
- }
- @Test
- /**
- * 获取表中所有记录条数
- */
- public void count() {
- System.out.println(MongoUtil.getColl("wujintao").getCount());
- System.out.println(MongoUtil.getColl("wujintao").count());
- }
- @Test
- /**
- * 获取查询结果集的记录数
- */
- public void getCount() {
- DBObject query = new BasicDBObject("name", "a");
- long count = MongoUtil.getColl("wujintao").count(query);
- System.out.println(count);
- }
- @Test
- /**
- * 查询所有结果
- */
- public void getAllDocuments() {
- DBCursor cursor = MongoUtil.getColl("wujintao").find();
- try {
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- } finally {
- cursor.close();
- }
- }
- @Test
- /**
- * 按照一个条件查询
- */
- public void queryByConditionOne() {
- BasicDBObject query = new BasicDBObject();
- query.put("name", "MongoDB");
- DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
- try {
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- } finally {
- cursor.close();
- }
- }
- @Test
- /**
- * AND多条件查询,区间查询
- */
- public void queryMulti() {
- BasicDBObject query = new BasicDBObject();
- // 查询j不等于3,k大于10的结果集
- query.put("j", new BasicDBObject("$ne", 3));
- query.put("k", new BasicDBObject("$gt", 10));
- DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
- try {
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- } finally {
- cursor.close();
- }
- }
- @Test
- /**
- * 区间查询
- * select * from table where i >50
- */
- public void queryMulti2() {
- BasicDBObject query = new BasicDBObject();
- query = new BasicDBObject();
- query.put("i", new BasicDBObject("$gt", 50)); // e.g. find all where i >
- DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
- try {
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- } finally {
- cursor.close();
- }
- }
- @Test
- /**
- * 区间查询
- * select * from table where 20 < i <= 30
- //比较符
- //"$gt": 大于
- //"$gte":大于等于
- //"$lt": 小于
- //"$lte":小于等于
- //"$in": 包含
- */
- public void queryMulti3() {
- BasicDBObject query = new BasicDBObject();
- query = new BasicDBObject();
- query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30));
- DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
- try {
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- } finally {
- cursor.close();
- }
- }
- /**
- * 组合in和and select * from test_Table where (a=5 or b=6) and (c=5 or d = 6)
- */
- public void queryMulti4() {
- BasicDBObject query11 = new BasicDBObject();
- query11.put("a", 1);
- BasicDBObject query12 = new BasicDBObject();
- query12.put("b", 2);
- List<BasicDBObject> orQueryList1 = new ArrayList<BasicDBObject>();
- orQueryList1.add(query11);
- orQueryList1.add(query12);
- BasicDBObject orQuery1 = new BasicDBObject("$or", orQueryList1);
- BasicDBObject query21 = new BasicDBObject();
- query21.put("c", 5);
- BasicDBObject query22 = new BasicDBObject();
- query22.put("d", 6);
- List<BasicDBObject> orQueryList2 = new ArrayList<BasicDBObject>();
- orQueryList2.add(query21);
- orQueryList2.add(query22);
- BasicDBObject orQuery2 = new BasicDBObject("$or", orQueryList2);
- List<BasicDBObject> orQueryCombinationList = new ArrayList<BasicDBObject>();
- orQueryCombinationList.add(orQuery1);
- orQueryCombinationList.add(orQuery2);
- BasicDBObject finalQuery = new BasicDBObject("$and",
- orQueryCombinationList);
- DBCursor cursor = MongoUtil.getColl("wujintao").find(finalQuery);
- }
- @Test
- /**
- * 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 = MongoUtil.getColl("wujintao").find(
- new BasicDBObject("name", in));
- try {
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- } finally {
- cursor.close();
- }
- }
- @Test
- /**
- * 或查询
- * 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 = MongoUtil.getColl("wujintao").find(query.get()).addSpecial("$returnKey", "");
- try {
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- } finally {
- cursor.close();
- }
- }
- @Test
- 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);
- }
- }
- @Test
- 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.put("pages", 400);
- books.insert(book);
- book = new BasicDBObject();
- book.put("name", "Understanding Axis2");
- book.put("pages", 150);
- books.insert(book);
- String map = "function() { "+
- "var category; " +
- "if ( this.pages >= 250 ) "+
- "category = 'Big Books'; " +
- "else " +
- "category = 'Small Books'; "+
- "emit(category, {name: this.name});}";
- String reduce = "function(key, values) { " +
- "var sum = 0; " +
- "values.forEach(function(doc) { " +
- "sum += 1; "+
- "}); " +
- "return {books: sum};} ";
- MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,
- null, MapReduceCommand.OutputType.INLINE, null);
- MapReduceOutput out = books.mapReduce(cmd);
- for (DBObject o : out.results()) {
- System.out.println(o.toString());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Test
- public void GroupByManyField() throws UnknownHostException{
- //此方法没有运行成功
- Mongo mongo = new Mongo("localhost", 27017);
- DB db = mongo.getDB("libary");
- DBCollection books = db.getCollection("books");
- BasicDBObject groupKeys = new BasicDBObject();
- groupKeys.put("total", new BasicDBObject("$sum","pages"));
- BasicDBObject condition = new BasicDBObject();
- condition.append("pages", new BasicDBObject().put("$gt", 0));
- String reduce = "function(key, values) { " +
- "var sum = 0; " +
- "values.forEach(function(doc) { " +
- "sum += 1; "+
- "}); " +
- "return {books: sum};} ";
- /**
- BasicDBList basicDBList = (BasicDBList)db.getCollection("mongodb中集合编码或者编码")
- .group(DBObject key, --分组字段,即group by的字段
- DBObject cond, --查询中where条件
- DBObject initial, --初始化各字段的值
- String reduce, --每个分组都需要执行的Function
- String finial --终结Funciton对结果进行最终的处理
- */
- DBObject obj = books.group(groupKeys, condition, new BasicDBObject(), reduce);
- System.out.println(obj);
- AggregationOutput ouput = books.aggregate(new BasicDBObject("$group",groupKeys));
- System.out.println(ouput.getCommandResult());
- System.out.println(books.find(new BasicDBObject("$group",groupKeys)));
- }
- @Test
- /**
- * 分页查询
- */
- public void pageQuery() {
- DBCursor cursor = MongoUtil.getColl("wujintao").find().skip(0)
- .limit(10);
- while (cursor.hasNext()) {
- System.out.println(cursor.next());
- }
- }
- /**
- * 模糊查询
- */
- public void likeQuery() {
- Pattern john = Pattern.compile("joh?n");
- BasicDBObject query = new BasicDBObject("name", john);
- // finds all people with "name" matching /joh?n/i
- DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
- }
- @Test
- /**
- * 条件删除
- */
- public void delete() {
- BasicDBObject query = new BasicDBObject();
- query.put("name", "xxx");
- // 找到并且删除,并返回删除的对象
- DBObject removeObj = MongoUtil.getColl("wujintao").findAndRemove(query);
- System.out.println(removeObj);
- }
- @Test
- /**
- * 更新
- */
- public void update() {
- BasicDBObject query = new BasicDBObject();
- query.put("name", "liu");
- DBObject stuFound = MongoUtil.getColl("wujintao").findOne(query);
- stuFound.put("name", stuFound.get("name") + "update_1");
- MongoUtil.getColl("wujintao").update(query, stuFound);
- }
- }
这里面涉及到的MongoUtil.java如下:
- package com.wujintao.mongo;
- import java.net.UnknownHostException;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.Mongo;
- /**
- * to see:http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency
- * Mongo工具类:设计为单例模式,每当月份发生变化,数据库连接名称就会发生变化,这是业务规则
- * 因 MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为10个)。
- * 对于有大量写和读的环境中,为了确保在一个Session中使用同一个DB时,我们可以用以下方式保证一致性:
- * DB mdb = mongo.getDB('dbname');
- * mdb.requestStart();
- * // 业务代码
- * mdb.requestDone();
- * DB和DBCollection是绝对线程安全的
- * @author wujintao
- */
- public class MongoUtil{
- private static Mongo mongo;
- private static DBCollection coll;
- private static Log log = LogFactory.getLog(MongoUtil.class);
- private static DB db;
- static{
- try {
- MongoOptions options = new MongoOptions();
- options.autoConnectRetry = true;
- options.connectionsPerHost = 1000;
- options.maxWaitTime = 5000;
- options.socketTimeout = 0;
- options.connectTimeout = 15000;
- options.threadsAllowedToBlockForConnectionMultiplier = 5000;
- //事实上,Mongo实例代表了一个数据库连接池,即使在多线程的环境中,一个Mongo实例对我们来说已经足够了
- mongo = new Mongo(new ServerAddress(DBMongoConfig.getHost(),DBMongoConfig.getPort()),options);
- //mongo = new Mongo(DBMongoConfig.getHost(),DBMongoConfig.getPort());
- // or, to connect to a replica set, supply a seed list of members
- // Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost",
- // 27017),
- // new ServerAddress("localhost", 27018),
- // new ServerAddress("localhost", 27019)));
- // 注意Mongo已经实现了连接池,并且是线程安全的。
- // 大部分用户使用mongodb都在安全内网下,但如果将mongodb设为安全验证模式,就需要在客户端提供用户名和密码:
- // boolean auth = db.authenticate(myUserName, myPassword);
- } catch (UnknownHostException e) {
- log.info("get mongo instance failed");
- }
- }
- public static DB getDB(){
- if(db==null){
- db = mongo.getDB(DBMongoConfig.getDbname());
- }
- return db;
- }
- public static Mongo getMong(){
- return mongo;
- }
- public static DBCollection getColl(String collname){
- return getDB().getCollection(collname);
- }
- }
MongoDB基本用法(增删改高级查询、mapreduce)的更多相关文章
- 封装对MongoDB数据库的增删改查访问方法(基于MongoDB官方发布的C#驱动)
本文利用MongoDB官方发布的C#驱动,封装了对MongoDB数据库的增删改查访问方法.先用官方提供的mongo-csharp-driver ,当前版本为1.7.0.4714 编写数据库访问帮助类 ...
- 利用koa实现mongodb数据库的增删改查
概述 使用koa免不了要操纵数据库,现阶段流行的数据库是mongoDB,所以我研究了一下koa里面mongoDB数据库的增删改查,记录下来,供以后开发时参考,相信对其他人也有用. 源代码请看:我的gi ...
- MongoDB --- 02. 基本操作,增删改查,数据类型,比较符,高级用法,pymongo
一.基本操作 . mongod 启动服务端 2. mongo 启动客户端 3. show databses 查看本地磁盘的数据库 4. use 库名 切换到要使用的数据库 5. db 查看当前使用的数 ...
- webpack4+express+mongodb+vue 实现增删改查
在讲解之前,我们先来看看效果如下所示: 1)整个页面的效果如下: 2) 新增数据效果如下: 3) 新增成功如下: 4) 编辑数据效果如下: 5) 编辑成功效果如下: 6) 删除数据效果如下: 7) 删 ...
- MongoDB学习之--增删改查(1)
本文是对mongodb学习的一点笔记,主要介绍最简单的增删改操作,初学,看着API,有什么错误,希望大家指正:(使用官方驱动) 1.增 增加操作是最简单的,构造bsonDcument插入即可: 方式1 ...
- python连接集群mongodb,封装增删改查
1.下载pymongo pip install pymongo 2.直接上代码 [ini配置文件] 封装读ini省略~~ [db.py] class Database(): def __init__( ...
- nodejs对mongodb数据库的增删改查操作(转载)
首先要确保mongodb的正确安装,安装参照:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-l ...
- 69.nodejs对mongodb数据库的增删改查操作
转自:https://www.cnblogs.com/sexintercourse/p/6485381.html 首先要确保mongodb的正确安装,安装参照:http://docs.mongodb. ...
- javascript数组的增删改和查询
数组的增删改操作 对数组的增删改操作进行总结,下面(一,二,三)是对数组的增加,修改,删除操作都会改变原来的数组. (一)增加 向末尾增加 push() 返回新增后的数组长度 arr[arr.leng ...
随机推荐
- Spring jndi数据源配置方法
xml配置: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana ...
- jQuery操作标签--样式、文本、属性操作, 文档处理
1.样式 2.文本 3.属性操作 全选,反选效果 4.文档处理 操作标签 一.样式操作 样式类: addClass(); // 添加指定的css类名 removeClass(); //移除指定的css ...
- Writing analyzers
Writing analyzers There are times when you would like to analyze text in a bespoke fashion, either b ...
- iOS 一张图片引发的崩溃SEGV_ACCERR
出错日志一直报SEGV_ACCERR,原因原来是第三方库SDWebImage下载图片,远程图片的json文件不对导致的闪退 解决方法: 1.command + B 编译工程(最好在编译工程时,清除下缓 ...
- iOS学习笔记(3)--初识UINavigationController(无storyboard)
纯代码创建导航控制器UINavigationController 在Xcode6.1中创建single view application的项目,删除Main.storyboard文件,删除info.p ...
- leetcode-733-Flood Fill
题目描述: An image is represented by a 2-D array of integers, each integer representing the pixel value ...
- 图的最短路径---弗洛伊德(Floyd)算法浅析
算法介绍 和Dijkstra算法一样,Floyd算法也是为了解决寻找给定的加权图中顶点间最短路径的算法.不同的是,Floyd可以用来解决"多源最短路径"的问题. 算法思路 算法需要 ...
- Linux Shell编程、变量、控制语句
为什么要学习Shell编程 1)Linux运维工程师在进行服务器集群管理时,需要编写Shell程序来进行服务器管理. 2)对于JavaEE和Python程序员来说,工作的需要,你的老大会要求你编写一些 ...
- flask _bootstrap中使用flash
在模板中获取flash闪现的那段代码要和内容块放在同一级别上.不然网页上是看不到闪现的内容的. 比如在基模板里定义一个content block ,里面一个是get_flashed_messages代 ...
- ANR机制
1.什么是ANR(Application Not Responding) 在Android中,应用程序的响应性是由ActivityManager和WindowManager系统服务监视的.当监测到以下 ...