完整工程下载地址:http://download.csdn.net/detail/linyu19872008/6913607
工程目录结构如图:
mongodb数据库管理类:
import com.mongodb.Mongo;
/**
* mongodb管理器
*
* @author jacklin
*
*/
public class MongodbManager {
private static Mongo mg = null;
/**
* 获取mongodb
*
* @return
*/
public synchronized static Mongo getMongo() {
if (mg == null) {
try {
mg = new Mongo("localhost", 27017);
} catch (Exception e) {
e.printStackTrace();
}
}
return mg;
}
}
|
mongodb测试用例:
import java.util.ArrayList;
import java.util.List;
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.util.JSON;
import com.sun.mongodb.manager.MongodbManager;
/**
* mongodb测试用例
*
* @author jacklin
*
*/
public class Testcase {
public static void main(String[] args) {
// // 查询所有的Database
// showDatabase();
// // 查询所有的聚集集合
// showCollections();
// // 查询数据
// find();
// // 添加数据
// add();
// // 添加多条数据
// addList();
// // 删除数据
// remove();
// // 修改数据
// modify();
}
/**
* 修改数据
*/
public static void modify() {
Mongo mg = MongodbManager.getMongo();
DB db = mg.getDB("test");
DBCollection users = db.getCollection("user");
DBObject userOld = new BasicDBObject();
userOld.put("name", "jack");
userOld.put("age", 24);
DBObject userNew = new BasicDBObject();
userNew.put("name", "jacky");
userNew.put("age", 26);
System.out.println(users.update(userOld, userNew));
}
/**
* 删除数据
*/
public static void remove() {
Mongo mg = MongodbManager.getMongo();
DB db = mg.getDB("test");
DBCollection users = db.getCollection("user");
DBObject user = new BasicDBObject();
user.put("name", "hoojo");
System.out.println(users.remove(user));
}
/**
* 添加多条数据
*/
public static void addList() {
Mongo mg = MongodbManager.getMongo();
DB db = mg.getDB("test");
DBCollection users = db.getCollection("user");
List<DBObject> list = new ArrayList<DBObject>();
DBObject user1 = new BasicDBObject("name", "lucy");
user1.put("age", 21);
DBObject user2 = new BasicDBObject("name", "lily");
user2.put("age", 22);
list.add(user1);
list.add(user2);
// 添加List集合
System.out.println(users.insert(list).getN());
}
/**
* 添加单条数据
*/
public static void add() {
Mongo mg = MongodbManager.getMongo();
DB db = mg.getDB("test");
DBCollection users = db.getCollection("user");
DBObject user = new BasicDBObject();
user.put("name", "jack");
user.put("age", 24);
System.out.println(users.save(user).getN());
}
/**
* 查询数据
*/
public static void find() {
Mongo mg = MongodbManager.getMongo();
DB db = mg.getDB("test");
DBCollection users = db.getCollection("user");
// 查询所有的数据
DBCursor cur = users.find();
while (cur.hasNext()) {
System.out.println("data:" + cur.next());
}
System.out.println("count:" + cur.count());
System.out.println("cursorId:" + cur.getCursorId());
System.out.println("json:" + JSON.serialize(cur));
}
/**
* 查询所有的聚集集合
*/
public static void showCollections() {
Mongo mg = MongodbManager.getMongo();
DB db = mg.getDB("test");
for (String name : db.getCollectionNames()) {
System.out.println("collectionName: " + name);
}
}
/**
* 查询所有的Database
*/
public static void showDatabase() {
Mongo mg = MongodbManager.getMongo();
for (String name : mg.getDatabaseNames()) {
System.out.println("dbName: " + name);
}
}
}
|
最新内容请见作者的GitHub页:http://qaseven.github.io/