欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

MongoDBJava连接

程序员文章站 2022-06-06 19:44:30
...

Java 连接MongoDB package mymaven;import java.net.UnknownHostException;import java.util.Set;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;impor

Java 连接MongoDB

package mymaven;

import java.net.UnknownHostException;
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 Test {
	/**
	 * @author ithomer.net
	 * @datetime 2013-11-11
	 */
	public static void main(String[] args) throws UnknownHostException, MongoException {
		Mongo mongo = new Mongo("172.27.9.104", 27017);		// 连接数据库
		DB db = mongo.getDB("mytestdb");					// 数据库
		Set cols = db.getCollectionNames();			// 获取数据库中所有的集合(类似于获取关系数据库中的表)
		
		// 打印出数据库中的集合,这里应当为null
		for (String s : cols) {
			System.out.println(s);
		}
		
		DBCollection collection = db.getCollection("mytestcoll");		// 创建一个集合
		collection.drop();												// 删除集合,插入数据时自动重建
		BasicDBObject obj = new BasicDBObject();						// 初始化一个基本DB对象,插入数据库的就是DB对象
		
		obj.put("from", "blog.ithomer.net");		// 放入几个键值对
		obj.put("to", "forum.ithomer.net");
		obj.put("subject", "ithomer.net");
		collection.insert(obj);						// 插入对象
		
		DBObject dbobj = collection.findOne();		// 查看一条记录,findOne()=find().limit(1);
		System.out.println(dbobj);					// 打印出刚才插入的数据
		
		// 插入10条{ranking:i}的数据
		for (int i = 0; i 

运行结果:

mytestcoll
system.indexes
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10107"} , "from" : "blog.ithomer.net" , "to" : "forum.ithomer.net" , "subject" : "ithomer.net"}
count: 11
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10107"} , "from" : "blog.ithomer.net" , "to" : "forum.ithomer.net" , "subject" : "ithomer.net"}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10108"} , "ranking" : 0}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10109"} , "ranking" : 1}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010a"} , "ranking" : 2}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010b"} , "ranking" : 3}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010c"} , "ranking" : 4}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010d"} , "ranking" : 5}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010e"} , "ranking" : 6}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010f"} , "ranking" : 7}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10110"} , "ranking" : 8}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10111"} , "ranking" : 9}
collection find({"ranking":1}):
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10109"} , "ranking" : 1}
collection find({"ranking":[5-9)}):
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010d"} , "ranking" : 5}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010e"} , "ranking" : 6}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd1010f"} , "ranking" : 7}
{ "_id" : { "$oid" : "52c62ed8e4b0f4de3dd10110"} , "ranking" : 8}

源码下载(maven)

mongodb数组示例:

	@SuppressWarnings("unchecked")
	public static void loadMediaTags(List mediaEntityList) {
		mediaEntityList.clear();
		
		try {
			Mongo mongo = new Mongo(CosineCluster.gMongo_HOST, CosineCluster.gMongo_PORT);
			DB db = mongo.getDB(CosineCluster.gMongo_DB);
			DBCollection collection = db.getCollection(CosineCluster.gMongo_Coll_Media);
			
			DBCursor cursor = collection.find();
			int index = 0;
			long startTime = System.currentTimeMillis();
			while(cursor.hasNext()){
				BasicDBObject obj = (BasicDBObject) cursor.next();
				
				long id = obj.getLong("_id");
				ArrayList tagList = (ArrayList) obj.get("tag");					// tag
				ArrayList keywordList = (ArrayList)obj.get("keyword");			// keyword
				ArrayList copyrightList = (ArrayList)obj.get("copyright");	// copyright
				
		
				MediaEntity mediaEntity = new MediaEntity();
				
				mediaEntity.setId(id);
				
				// tag
				for(int j=0; j actorsObjList = (ArrayList)obj.get("actors");		// actors
				for(int j=0; j directorObjList = (ArrayList)obj.get("director");	// director
				for(int j=0; j 100) {
					break;
				}
				System.out.println(index + " --- mediaEntity : " + mediaEntity.toString());
			}
			long costTime = System.currentTimeMillis() - startTime;
			System.out.println("load data costTime = " + index + "; costTime = " + costTime/1000f);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static int getTagInt(String tag) {
		int tagIntId = -1;
		
		try {
			MongoClient mongo = new MongoClient(CosineCluster.gMongo_HOST, CosineCluster.gMongo_PORT);
			DB db = mongo.getDB(CosineCluster.gMongo_DB);
			DBCollection tagmapCollection = db.getCollection("recommend_tag_map");

			DBCursor cursor = tagmapCollection.find(new BasicDBObject("name", tag));	
			if(cursor == null || cursor.toArray().size()