分享一下我经常用到的自己写的mongo用法示例
该示例基于当前最新的mongo驱动,版本为mongo-2.10.1.jar,用junit写的单元测试。
?
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 { @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的结