MongoDB java api for 插入和单collection基本查询使用示例
2011-05-31 16:49
1046 查看
原文地址:http://blog.csdn.net/tujiyue/archive/2011/05/21/6436312.aspx
1.首先为我们要准备操作的数据库添加一个用户验证:
2. 下载MongoDB的java api包,mongo-2.5.3.jar,建立一个java工程如下:
3. 示例代码:
4. 运行结果:
1.首先为我们要准备操作的数据库添加一个用户验证:
[root@localhost src]# mongo MongoDB shell version: 1.8.1 connecting to: test > show dbs; admin (empty) local (empty) > db test > db.addUser("iwtxokhtd","123456"); { "user" : "iwtxokhtd", "readOnly" : false, "pwd" : "c728e00401a72282a2919648723dbff7" } > show collections; system.indexes system.users > db.system.users.find(); { "_id" : ObjectId("4dd73c7d247cb75e4995757b"), "user" : "iwtxokhtd", "readOnly" : false, "pwd" : "c728e00401a72282a2919648723dbff7" } >
2. 下载MongoDB的java api包,mongo-2.5.3.jar,建立一个java工程如下:
3. 示例代码:
/** * MongoDBTest * MongoDB java api的初步使用示例 * 此次只介绍一下insert和query(基本单collection查询)两种操作 */ package com.labci.mongodb.test; import java.net.UnknownHostException; import java.util.Iterator; import java.util.regex.Pattern; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * @author Bill Tu(tujiyue/iwtxokhtd) * May 21, 2011[12:06:41 PM] * */ public class MongoDBJavaAPITest { private static final String HOST = "192.168.1.86"; private static final int PORT = 27017; private static final String USER = "iwtxokhtd"; private static final String PASSWORD = "123456"; private static final String DB_NAME = "test"; private static final String COLLECTION = "data_test"; private static final int SIZE = 10; /** * 进行测试 * @throws Exception */ private static void initTest() throws Exception{ try { Mongo conn=new Mongo(HOST,PORT);//建立数据库连接 DB testDB=conn.getDB(DB_NAME);//取得test数据库 /** * 如果test数据库没有设定用户权限认证,则无需下面的验证 */ boolean loginSuccess=testDB.authenticate(USER, PASSWORD.toCharArray()); if(!loginSuccess){ throw new Exception("登录"+DB_NAME+"验证失败,请确认用户名和密码"); } /** * 如果COLLECTION不存在,则MongoDB会自动为你创建此collection */ DBCollection collection=testDB.getCollection(COLLECTION); //开始插入数据操作 insertData(collection,SIZE); //查询操作 findData(collection); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } /** * 向collection插入size条记录 */ private static void insertData(DBCollection collection,int size){ long beginTime=System.currentTimeMillis(); for(int i=1;i<=size;i++){ BasicDBObject basic=new BasicDBObject(); basic.put("userId", "1001017"+i); basic.put("userName", "Bill Tu"+i); basic.put("gender", "m"+i); BasicDBObject interests=new BasicDBObject(); interests.put("game", "game"+i); interests.put("ball", "ball"+i); interests.put("other", "nothing"+i); basic.put("interests", interests); collection.insert(basic); } long endTime=System.currentTimeMillis(); System.out.println("插入用时:"+(endTime-beginTime)+" ms"); } /** * 根据指定collection单collection查询 * */ private static void findData(DBCollection collection){ //查询所有记录 long beginTime=System.currentTimeMillis(); DBCursor queryAll=collection.find(); System.out.println("所有记录:"); for(Iterator<DBObject> iter=queryAll.iterator();iter.hasNext();){ System.out.println(iter.next()); } long endTime=System.currentTimeMillis(); System.out.println("查询所有记录用时:"+(endTime-beginTime)+" ms"); //只看第一条记录 DBObject queryFirstRecord=collection.findOne(); System.out.println("第一条记录:"+queryFirstRecord); //根据单条件查询 DBObject singleCondition_query=new BasicDBObject(); //根据userId=10010172条件来查 singleCondition_query.put("userId", "10010172"); DBCursor singleQueryResult=collection.find(singleCondition_query); for(Iterator<DBObject> iter=singleQueryResult.iterator();iter.hasNext();){ System.out.println("按单条件查询结果:"+iter.next()); } //根据复合条件来查询 DBObject compoundCondition_query=new BasicDBObject(); //根据userId=10010171&userName=Bill Tu1来查询 compoundCondition_query.put("userId", "10010171"); compoundCondition_query.put("userName", "Bill Tu1"); DBCursor compoundQueryResult=collection.find(compoundCondition_query); System.out.println("按复合条件查询结果:"); for(Iterator<DBObject> iter=compoundQueryResult.iterator();iter.hasNext();){ System.out.println(iter.next()); } //in查询 DBObject in_data=new BasicDBObject("$in",new Object[]{"10010171","10010172"}); //根据userId in('10010171','10010172')查询 DBObject in_query=new BasicDBObject(); in_query.put("userId", in_data); DBCursor inQueryResult=collection.find(in_query); System.out.println("按in条件查询结果:"); for(Iterator<DBObject> iter=inQueryResult.iterator();iter.hasNext();){ System.out.println(iter.next()); } //模糊查询 DBObject fuzzy_query=new BasicDBObject(); String keyWord="10010171"; Pattern pattern = Pattern.compile("^" + keyWord + ".*$", Pattern.CASE_INSENSITIVE); //根据userId like 1001017%查询 fuzzy_query.put("userId", pattern); DBCursor fuzzyQueryResult=collection.find(fuzzy_query); System.out.println("按模糊条件查询结果:"); for(Iterator<DBObject> iter=fuzzyQueryResult.iterator();iter.hasNext();){ System.out.println(iter.next()); } } /** * @param args */ public static void main(String[] args) { try { initTest(); } catch (Exception e) { e.printStackTrace(); } } }
4. 运行结果:
插入记录用时:16 ms 所有记录: { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a30"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a31"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a32"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a33"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a34"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a35"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a36"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a37"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a38"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a39"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}} 查询所有记录用时:16 ms 第一条记录:{ "_id" : { "$oid" : "4dd747cb3a491e68a1a84a30"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} 按单条件查询结果:{ "_id" : { "$oid" : "4dd747cb3a491e68a1a84a31"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} 按复合条件查询结果: { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a30"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} 按in条件查询结果: { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a30"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a31"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} 按模糊条件查询结果: { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a30"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} { "_id" : { "$oid" : "4dd747cb3a491e68a1a84a39"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}
相关文章推荐
- MongoDB java api for 插入和单collection基本查询使用示例
- MongoDB java api for 插入和单collection基本查询使用示例
- MongoDB Java API for 插入和单collection基本查询使用示例
- MongoDB JAVA API高级查询示例(对应于上一篇)
- MongoDB JAVA API插入和删除示例
- $within进行空间查询--MongoDB API for Java
- mongodb基本命令和Java操作API示例
- 在使用dhtmlxconnection for java时如何动态传入查询参数
- ArcGIS API For Javascript之地图基本加载与显示,地图切换、缩放、定位、比例尺、鹰眼图、坐标显示、查询搜索功能实现
- Java操作mongoDB2.6的常见API使用方法
- HBase总结(十一)hbase Java API 介绍及使用示例
- hbase Java API 介绍及使用示例 (总结10)
- java查询mongodb中的objectid示例
- ArcGIS API for JavaScript开发初探——基本地图组件使用
- mongodb- Java API 查询操作
- MongoDB API java的使用
- 整理Java的MyBatis框架中一些重要的功能及基本使用示例
- 使用java连接mysql数据库进行数据的查询插入更新及删除
- ArcGIS API for Javascript 使用缓冲区结果做query查询出现“esri.config.defaults.io.proxyUrl 尚未进行设置”错误
- java使用mongodb API操作MongoDB