java操作 mongo DB 資料庫例子

瓜瓜東西發表於2014-09-17
package com.tompig.ljc;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
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;

public class DBUtil {
	String collectionsName = "user_info";
	String dbName = "ljc";
	DBCollection coll = null;
	Mongo m = null;

	public DBUtil() {
		try {
			coll = getDBCollection(collectionsName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DBCollection getDBCollection(String collectionName) {
		DBCollection coll = null;
		try {
			m = new Mongo("localhost", 27017);
			DB db = m.getDB(dbName);
//			if (db.authenticate("ljc", "007".toCharArray())) {
//				System.out.println("success");
//			}
			coll = db.getCollection(collectionName);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
		return coll;
	}

	// mongo db 刪除資料庫
	public void dropDatabase() {
		try {
			m.dropDatabase(dbName);
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}

	// mongo db查詢所有表名
	public void getAllCollections() {
		DB db = m.getDB("mydb");
		Set<String> colls = db.getCollectionNames();
		for (String s : colls) {
			System.out.println(s);
		}
	}

	// mongo db 查詢索引
	public void getAllIndex() {
		List<DBObject> list = coll.getIndexInfo();
		for (DBObject o : list) {
			System.out.println(o);
		}
	}

	// mongo db 插入 insert
	public void insert(int i) {
		BasicDBObject doc = new BasicDBObject();
		doc.put("name", "liu" + i);
		doc.put("age", 20 + i);
		doc.put("sex", "boy");
		doc.put("time", new Date());
		coll.insert(doc);
	}

	public void batchInsert() {
		List datas = new ArrayList();
		for (int i = 0; i < 100; i++) {
			BasicDBObject bo = new BasicDBObject();
			bo.put("name", "liu");
			bo.append("age", i);
			datas.add(bo);
		}
		coll.insert(datas);
	}

	// 查詢單個
	public void findOne() {
		BasicDBObject obj = (BasicDBObject) coll.findOne();
		System.out.println(obj);
	}

	// mongo db 修改
	public void update() {
		BasicDBObject query = new BasicDBObject();
		query.put("name", "liu");
		DBObject stuFound = coll.findOne(query);
		stuFound.put("name", stuFound.get("name") + "update_1");
		coll.update(query, stuFound);
	}

	// 查詢所有
	public void queryAll() {
		BasicDBObject obj = new BasicDBObject();
		obj.put("sex", "boy");
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			String name = (String) cursor.next().get("name");
			System.out.println(name);
		}
		cursor.close();
	}

	// 條件刪除
	public void delete(String name) {
		BasicDBObject query = new BasicDBObject();
		query.put("name", name);
		// 找到並且刪除,並返回刪除的物件
		DBObject removeObj = coll.findAndRemove(query);
		System.out.println(removeObj);
	}

	// 條件查詢
	public void findByName(String name) {
		BasicDBObject obj = new BasicDBObject();
		obj.put("name", name);
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}
//條件查詢2
	public void argsFind(){
		BasicDBObject condition = new BasicDBObject();
		//condition.put("age", new BasicDBObject("$gt", 20).append("$lte", 30)); 
		condition.put("age", new BasicDBObject("$gt", 50));
		coll.find(condition);
		//比較符
		//"$gt": 大於
		//"$gte":大於等於
		//"$lt": 小於
		//"$lte":小於等於
		//"$in": 包含
		//以下條件查詢20<age<=30
	}
	
	// 分頁查詢
	public void pageQuery() {
		DBCursor cursor = coll.find().skip(0).limit(10);
		while (cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}
}


 

相關文章