当前位置:首页 > 开发 > 数据库 > 正文

java中使用MongoDB

发表于: 2014-06-05   作者:功夫小当家   来源:转载   浏览:
摘要:    今天研究了下java如何使用MongoDB。    驱动下载地址:https://github.com/mongodb/mongo-java-driver/downloads    代码如下: package mongo; import java.net.UnknownHostException; import jav

   今天研究了下java如何使用MongoDB。

   驱动下载地址:https://github.com/mongodb/mongo-java-driver/downloads

   代码如下:

package mongo;

import java.net.UnknownHostException;
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.WriteResult;

public class MongoTest {

	private static Mongo mongo;
	private static DB db;
	private static DBCollection collection;
	
	public static void init() {
		 try {
			mongo = new Mongo(" 192.168.134.15", 27017);
			//test和collection如果在数据库中没有,则会自动建立 
			db = mongo.getDB("test");
			collection = db.getCollection("collection");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		init();
		MongoTest test = new MongoTest();
		test.insertMulti();
		DBCursor cursor = test.iteratorAllCusor();
		test.getCursor(cursor);
		test.dropDatabase("test");
	}
	
	public void dropDatabase(String dbName){
		mongo.dropDatabase(dbName);
	}
	
	//插入一条
	public void insert() {
		BasicDBObject tester = new BasicDBObject();
		tester.put("name", "tester1");
		tester.put("age", 25);
		tester.put("score", 100);
		collection.insert(tester);
	}
	
	// 插入多条数据
	public void insertMulti() {
		for (int i = 0; i < 10; i++) {
			collection.insert(new BasicDBObject().append("i", i));
		}
	}
	
	//查找第一条数据 
	public DBObject findOne() {
		return collection.findOne();
	}
	
	// 使用Cursor 获取所有文档
	public DBCursor iteratorAllCusor() {
		DBCursor cursor = collection.find();
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
		return cursor;
	}
	
	//得到i = 5的cursor
	public void getCursor(DBCursor cursor) {
		BasicDBObject queryObj = new BasicDBObject();
		queryObj.put("i", 5);
		cursor = collection.find(queryObj);
		try {
			while (cursor.hasNext()) {
				System.out.println("当前的 cursor = " + cursor.next());
			}
		} finally {
			cursor.close();
		}
	}
	
	// 查找 1 < i <= 5
	public void queryDBObject(DBCursor cursor , BasicDBObject queryObj) {
		queryObj.put("i", new BasicDBObject("$gt", 1).append("$lte", 5));  
		cursor = collection.find(queryObj);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}
	
	//删除i = 5的项
	public void remove() {
		BasicDBObject remove = new BasicDBObject();
		remove.put("i", 5);
		WriteResult result = collection.remove(remove);
		System.out.println("删除i= 5的项" + result); 
	}
	
	//把i = 1 替换为 i= 2 
	public void update() {
		BasicDBObject queryObj = new BasicDBObject();
		queryObj.put("i", 1);
		BasicDBObject update = new BasicDBObject();
		update.put("i", 2);
		DBObject obj = collection.findAndModify(queryObj, update);
		System.out.println(obj);
	 
	}
	
	// 创建索引 1为升序、-1为降序
	public void createIndex() {
		collection.createIndex(new BasicDBObject("i", 1)); 
	}
	
	//获得索引列表
	public void getIndexList() {
		List<DBObject> list = collection.getIndexInfo();
		for (DBObject o : list) {
			System.out.println(o);
		}
	}
	
	// 获取数据库列表
	public void getDBList() {
		for (String s : mongo.getDatabaseNames()) {
			System.out.println(s);
		}
	}
	
	// 获取集合列表
	public void getCollectionsList() {
		Set<String> colls = db.getCollectionNames();
		for (String s : colls) {
			System.out.println(s);
		}
	}
}

 

java中使用MongoDB

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号