標籤:mongodb 代碼 學習 java 入門
原文:分享哈我測試MongoDB資料庫的簡單代碼
原始碼:http://www.zuidaima.com/share/1550463424236544.htm
分布式檔案儲存體系統,MongoDB部署安裝具體可以參照:
http://www.cnblogs.com/mecity/archive/2011/06/11/2078527.html
Demo實現了從txt檔案中逐行讀取資料,存放到MongoDB資料庫中的。
可參照其它Demo代碼
這是一套基於Spring, Hibernate, Spring Data, Akka, Twitter Bootstrap, Apache Tiles, jQuery的簡單案例源碼
http://www.zuidaima.com/share/1550463264246784.htm
分享java操作mongodb的crud代碼
代碼中包括了mongodb的crud及其分頁查詢,排序功能
http://www.zuidaima.com/share/1550463230446592.htm
package com.zuidaima.pb.test;/** * 測試MongoDB資料庫中查詢所花費的時間(ms) * @author zoe www.zuidaima.com * date 2012-12-26 */import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.net.UnknownHostException;import java.util.regex.Pattern;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.Mongo;public class MyMongoDB {/* * get DBCollction * @return */public static DBCollection getcoll() {Mongo mongo;try {mongo = new Mongo("localhost", 27017);DB db = mongo.getDB("working");DBCollection coll = db.getCollection("mytest");return coll;} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}/* * 該方法是把txt檔案中的資料,逐行存入到MongoDB資料庫中 * 且每行中的資料只放入了一個欄位中,若果是一行中的資料要放入多個欄位中的話,還沒有驗證 */public static void add() {String url = "d:/file.txt";try {FileReader read = new FileReader(new File(url));BufferedReader br = new BufferedReader(read);StringBuffer sbf = new StringBuffer("");String line = null;int i = 0;DBCollection coll = getcoll();while ((line = br.readLine()) != null) {coll.insert(new BasicDBObject().append("name", line).append("no", i++));}System.out.println(coll.getCount());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/* * 無索引查詢 */public static void query() {DBCollection coll=getcoll(); BasicDBObject cond = new BasicDBObject();Pattern pattern = Pattern.compile("浙江.*$");long a = System.currentTimeMillis();cond.put("name", pattern);// cond.put("name","寧海中通電子科技有限公司" );DBCursor returns = coll.find(cond);int i = 0;while (returns.hasNext() && (++i < 25)) {System.out.println(returns.next());}long b = System.currentTimeMillis();System.out.println("所用時間 " + (b - a) + "毫秒");}public static void main(String[] args) {query();}}
分享哈我測試MongoDB資料庫的簡單代碼