1.需要的包:mongo-2.10.1.jar(下载地址在http://www.mongodb.org/downloads)
2.使用方法:
MongoClient mongoClient = null;
try {
mongoClient = new MongoClient();
} catch (UnknownHostException e) {
e.printStackTrace();
}
// 获得数据库
DB db = mongoClient.getDB("cms");
// 获得表
DBCollection foo = db.getCollection("foo");
// 插入数据
DBObject arr = new BasicDBObject();
arr.put("bar", "baz");
foo.insert(arr);
// 查询数据
DBCursor cur = foo.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
PS:请确保mongoDB服务已开启(已运行mongod.exe)。