Java操作Mongodb

Java版本驱动下载地址:驱动下载地址

简单查询:

package mongoDb;

import java.text.MessageFormat;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;

/**
 * @mongo.java
 * @ @mongoDb
 * @author xiaonanhai
 * @2017年12月21日
 * @下午7:14:49
 * @TODO
 */

public class mongo {
	public static void main(String args[]) {
		try {
			// 连接到 mongodb 服务
			MongoClient mongoClient = new MongoClient("192.168.1.122", 27017);

			// 连接到数据库
			MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
			//MongoIterable listCollectionNames = mongoDatabase.listCollectionNames();
			 MongoCollection collection = mongoDatabase.getCollection("foo");
			 FindIterable findIterable = collection.find();  
	         MongoCursor mongoCursor = findIterable.iterator();  
	         while(mongoCursor.hasNext()){  
	            System.out.println(mongoCursor.next());  
	         }  

		} catch (Exception e) {
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
		}
	}
}


你可能感兴趣的:(mongoDb)