[转]MongoDB插入和查询操作
MongoDB下的高级查询示例 http://www.linuxidc.com/Linux/2011-05/36312.htm
MongoDB Java API for 插入和单collection基本查询使用示例 http://www.linuxidc.com/Linux/2011-05/36313.htm
MongoDB下的查询操作(与Java API查询操作对应) http://www.linuxidc.com/Linux/2011-05/36314.htm
View Code
1 /** 2 * MongoDBTest 3 * MongoDB java api的初步使用示例 4 * 此次只介绍一下insert和query(基本单collection查询)两种操作 5 */ 6 package com.labci.mongodb.test; 7 import java.net.UnknownHostException; 8 import java.util.Iterator; 9 import java.util.regex.Pattern; 10 import com.mongodb.BasicDBObject; 11 import com.mongodb.DB; 12 import com.mongodb.DBCollection; 13 import com.mongodb.DBCursor; 14 import com.mongodb.DBObject; 15 import com.mongodb.Mongo; 16 import com.mongodb.MongoException; 17 /** 18 * @author Bill Tu(tujiyue/iwtxokhtd) 19 * May 21, 2011[12:06:41 PM] 20 * 21 */ 22 public class MongoDBJavaAPITest { 23 private static final String HOST = "192.168.1.86"; 24 private static final int PORT = 27017; 25 private static final String USER = "iwtxokhtd"; 26 private static final String PASSWORD = "123456"; 27 private static final String DB_NAME = "test"; 28 private static final String COLLECTION = "data_test"; 29 private static final int SIZE = 10; 30 31 /** 32 * 进行测试 33 * @throws Exception 34 */ 35 private static void initTest() throws Exception{ 36 try { 37 Mongo conn=new Mongo(HOST,PORT);//建立数据库连接 38 DB testDB=conn.getDB(DB_NAME);//取得test数据库 39 /** 40 * 如果test数据库没有设定用户权限认证,则无需下面的验证 41 */ 42 boolean loginSuccess=testDB.authenticate(USER, PASSWORD.toCharArray()); 43 if(!loginSuccess){ 44 throw new Exception("登录"+DB_NAME+"验证失败,请确认用户名和密码"); 45 } 46 /** 47 * 如果COLLECTION不存在,则MongoDB会自动为你创建此collection 48 */ 49 DBCollection collection=testDB.getCollection(COLLECTION); 50 //开始插入数据操作 51 insertData(collection,SIZE); 52 //查询操作 53 findData(collection); 54 55 } catch (UnknownHostException e) { 56 e.printStackTrace(); 57 } catch (MongoException e) { 58 e.printStackTrace(); 59 } 60 } 61 62 /** 63 * 向collection插入size条记录 64 */ 65 private static void insertData(DBCollection collection,int size){ 66 long beginTime=System.currentTimeMillis(); 67 for(int i=1;i<=size;i++){ 68 BasicDBObject basic=new BasicDBObject(); 69 basic.put("userId", "1001017"+i); 70 basic.put("userName", "Bill Tu"+i); 71 basic.put("gender", "m"+i); 72 73 BasicDBObject interests=new BasicDBObject(); 74 interests.put("game", "game"+i); 75 interests.put("ball", "ball"+i); 76 interests.put("other", "nothing"+i); 77 78 basic.put("interests", interests); 79 80 collection.insert(basic); 81 } 82 long endTime=System.currentTimeMillis(); 83 System.out.println("插入用时:"+(endTime-beginTime)+" ms"); 84 85 } 86 87 /** 88 * 根据指定collection单collection查询 89 * 90 */ 91 private static void findData(DBCollection collection){ 92 //查询所有记录 93 long beginTime=System.currentTimeMillis(); 94 DBCursor queryAll=collection.find(); 95 System.out.println("所有记录:"); 96 for(Iterator<DBObject> iter=queryAll.iterator();iter.hasNext();){ 97 System.out.println(iter.next()); 98 } 99 long endTime=System.currentTimeMillis(); 100 System.out.println("查询所有记录用时:"+(endTime-beginTime)+" ms"); 101 102 //只看第一条记录 103 DBObject queryFirstRecord=collection.findOne(); 104 System.out.println("第一条记录:"+queryFirstRecord); 105 106 //根据单条件查询 107 DBObject singleCondition_query=new BasicDBObject(); 108 //根据userId=10010172条件来查 109 singleCondition_query.put("userId", "10010172"); 110 111 DBCursor singleQueryResult=collection.find(singleCondition_query); 112 for(Iterator<DBObject> iter=singleQueryResult.iterator();iter.hasNext();){ 113 System.out.println("按单条件查询结果:"+iter.next()); 114 } 115 116 //根据复合条件来查询 117 DBObject compoundCondition_query=new BasicDBObject(); 118 //根据userId=10010171&userName=Bill Tu1来查询 119 compoundCondition_query.put("userId", "10010171"); 120 compoundCondition_query.put("userName", "Bill Tu1"); 121 DBCursor compoundQueryResult=collection.find(compoundCondition_query); 122 System.out.println("按复合条件查询结果:"); 123 for(Iterator<DBObject> iter=compoundQueryResult.iterator();iter.hasNext();){ 124 System.out.println(iter.next()); 125 } 126 127 //in查询 128 DBObject in_data=new BasicDBObject("$in",new Object[]{"10010171","10010172"}); 129 //根据userId in('10010171','10010172')查询 130 DBObject in_query=new BasicDBObject(); 131 in_query.put("userId", in_data); 132 DBCursor inQueryResult=collection.find(in_query); 133 System.out.println("按in条件查询结果:"); 134 for(Iterator<DBObject> iter=inQueryResult.iterator();iter.hasNext();){ 135 System.out.println(iter.next()); 136 } 137 138 //模糊查询 139 DBObject fuzzy_query=new BasicDBObject(); 140 String keyWord="10010171"; 141 Pattern pattern = Pattern.compile("^" + keyWord + ".*$", Pattern.CASE_INSENSITIVE); 142 //根据userId like 1001017%查询 143 fuzzy_query.put("userId", pattern); 144 DBCursor fuzzyQueryResult=collection.find(fuzzy_query); 145 System.out.println("按模糊条件查询结果:"); 146 for(Iterator<DBObject> iter=fuzzyQueryResult.iterator();iter.hasNext();){ 147 System.out.println(iter.next()); 148 } 149 150 } 151 152 /** 153 * @param args 154 */ 155 public static void main(String[] args) { 156 try { 157 initTest(); 158 } catch (Exception e) { 159 e.printStackTrace(); 160 } 161 } 162 }