`

mongodb

 
阅读更多
 MongoDB基本用法(增删改高级查询

 // 该示例基于当前最新的mongo驱动,版本为mongo-2.10.1.jar,用junit写的单元测试

 

package com.dengqw.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);
	}
	
}

 

package com.dengqw.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);
	}
}

 

package com.dengqw.mongdb;

public class DBMongoConfig {
	
	protected static final String HostName = "127.0.0.1";
	protected static final int HostPort = 27017;
	protected static final String HostDbName = "wujintao";
	
	
	public static String getHost() {
		return HostName;
	}
	
	public static int getPort() {
		return HostPort;
	}
	
	
	public static String getDbname() {
		return HostDbName;
	}

}

 

 

分享到:
评论

相关推荐

    Linux安装mongodb客户端

    sudo vim /etc/yum.repos.d/mongodb-org-4.2.repo 写入: [mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck=1 enabled=1 gpg...

    mongodb安装包和compass

    MongoDB是一款开源、分布式、高性能的NoSQL数据库,它不使用传统的表格和列式结构来存储数据,而是采用键值对、文档、集合和图形数据模型。这种设计使得MongoDB在处理非结构化和半结构化数据时表现出色,特别适合大...

    mongodb-测试数据

    MongoDB是一种流行的开源、分布式文档数据库,常被用于构建高性能、可扩展的应用程序。这个“mongodb-测试数据”压缩包显然包含了一些用于测试MongoDB功能的样例数据集,特别是针对增、删、改、查(CRUD)操作的学习...

    linux安装mongodb教程

    /usr/local/mongodb/mongodb-linux-2.0.7/bin/mongod --dbpath=/usr/local/mongodb/data/db --logpath=/usr/local/mongodb/mongodb-linux-2.0.7/logs/mongodb.log --logappend --port=27017 --fork 知识点 6:配置...

    spring-data使用mongodbTemplate对MongoDB进行读写操作

    Spring Data MongoDB是一个强大的Java库,它为开发人员提供了一种简单的方式来访问和操作MongoDB数据库。这个库是Spring Data框架的一部分,旨在简化数据访问层的实现,尤其在使用NoSQL数据库如MongoDB时。MongoDB...

    MongoDB实验 - .docx

    MongoDB 实验报告 本实验报告旨在详细介绍 MongoDB 的安装、配置和基本操作步骤,本报告基于 CentOS 7 系统,通过一步一步的截图和文字说明,帮助读者快速掌握 MongoDB 的使用。 一、安装 MongoDB 首先,我们需要...

    MongoDB之conf配置文件详解

    MongoDB之conf配置文件详解 MongoDB的配置文件是服务器的核心组件之一,它控制着MongoDB服务器的各种设置和行为。在本文中,我们将详细介绍MongoDB的配置文件的各个部分,并解释每个设置的作用和意义。 一、数据库...

    mongodb c#驱动最新驱动mongodb.driver.dll 版本2.12.0-beta1

    MongoDB 是一个流行的开源、基于分布式文件存储的数据库系统,主要设计用于处理大量数据的分布式环境。C# 驱动是 MongoDB 提供的一种客户端库,允许 .NET 开发者与 MongoDB 数据库进行交互。标题提到的是 MongoDB 的...

    mongodb.dll 下载.zip

    MongoDB是一个开源、分布式、高性能的NoSQL数据库,以其灵活性、可扩展性和高可用性而闻名。`mongodb.dll`是MongoDB数据库系统在Windows平台上运行所必需的一个动态链接库(DLL)文件,它包含了MongoDB客户端和...

    geoserver发布mongodb矢量数据地图服务.docx

    Geoserver发布MongoDB矢量数据地图服务 Geoserver是一款功能强大且开源的地理信息系统(GIS)服务器,能够实现空间数据的存储、处理和发布。MongoDB是一款NoSQL数据库,能够存储大量的矢量数据。本文将介绍如何使用...

    MongoDB应用设计模式

    资源名称:MongoDB应用设计模式内容简介:无论是在构建社交媒体网站,还是在开发一个仅在内部使用的企业应用程序,《MongoDB应用设计模式》展示了MongoDB需要解决的商业问题之间的连接。你将学到如何把MongoDB设计...

    MongoDB(mongodb-org-server_5.0.4_amd64.deb)

    MongoDB Community Server(mongodb-org-server_5.0.4_amd64.deb)适用于适用于Debian10 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是...

    mongodb数据库jar包

    MongoDB是一个流行的开源、分布式文档型数据库,设计用于处理大量数据并提供高可用性和高性能。在Java应用程序中,为了与MongoDB进行交互,我们需要使用Java MongoDB驱动程序。这个压缩包包含的就是Java连接MongoDB...

    MongoDB4.2.21 Linux版本安装包

    MongoDB是一款高性能、无模式的分布式文档型数据库,被广泛应用于大数据分析、内容管理系统、物联网(IoT)、实时应用程序和地理位置数据存储等场景。在Linux环境下安装MongoDB 4.2.21版本,是许多系统管理员和开发者...

    MongoDB Community(mongodb-linux-aarch64-ubuntu1804-5.0.8.tgz)

    MongoDB Community Server(mongodb-linux-aarch64-ubuntu1804-5.0.8.tgz)适用于Ubuntu 18.04 Arm芯片, MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决...

    MongoDB(mongodb-src-r5.0.4.tar.gz)

    MongoDB Community Server(mongodb-src-r5.0.4.tar.gz)源代码 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非...

    MongoDB c#驱动 dll

    MongoDB是一种流行的开源、分布式文档型数据库,以其灵活性、高性能和可伸缩性而闻名。在C#开发环境中,MongoDB提供了专门的C#驱动程序,使得开发者能够方便地与MongoDB进行交互。本篇文章将深入探讨MongoDB的C#驱动...

    Mongodb基础知识详解(值得珍藏).pdf

    MongoDB 是一个高性能的NoSQL数据库,以分布式文件存储为基础,提供灵活的数据模型和高效的查询操作。MongoDB 不同于传统的关系型数据库,它采用面向集合的存储方式,支持无模式的数据模型,允许数据自由组织,这...

    Mongodb for Ubuntu 18.04 ARM 64

    MongoDB 是一个流行的开源文档型数据库,被广泛用于存储、管理和检索非结构化或半结构化数据。在本文中,我们将深入探讨如何在基于ARM架构的Ubuntu 18.04系统上安装和使用MongoDB。 一、ARM架构与Ubuntu 18.04 ARM...

    MongoDB Days 2015 深圳 PPT 共享

    MongoDB是一种分布式文档数据库,以其灵活性、高性能和可伸缩性而闻名,尤其适用于处理大量半结构化和非结构化数据。MongoDB Day 2015 深圳活动显然是一个专门针对MongoDB技术的研讨会或会议,旨在深入探讨和分享...

Global site tag (gtag.js) - Google Analytics