java操作mongodb示例2

public class SimpleCRUDTest {
	private Mongo mongo = null;
	private DB db = null;
	private DBCollection collection = null;
	public SimpleCRUDTest(){
		try {
			mongo = new Mongo();
			db = mongo.getDB("test");
			collection = db.getCollection("things");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public void queryAll(){
		DBCursor cursor = collection.find();
		while(cursor.hasNext()){
			System.out.println(cursor.next());
		}
	}
	
	public void simpleAdd(){
		DBObject object = new BasicDBObject();
		object.put("name", "long0416");
		object.put("age", 12);
		object.put("sex", "male");
		WriteResult result = collection.save(object);
		System.out.println("受影响条数: " + result.getN());
	}
	
	public void complexAdd(){
		List<DBObject> objectList = new ArrayList<DBObject>();
		DBObject object = new BasicDBObject();
		object.put("name", "longtest1");
		object.put("sex", "female");
		
		DBObject object2 = new BasicDBObject();
		object2.put("name", "longtest2");
		object2.put("age", 11);
		
		objectList.add(object);
		objectList.add(object2);
		
		collection.insert(objectList);
	}
	
	public void update(){
		BasicDBObject updateObject = new BasicDBObject();
		updateObject.put("x", 4.0);
		updateObject.put("j", "sss");
		WriteResult result = collection.update(new BasicDBObject("x", 4.0), updateObject);
		System.out.println("受影响条数:" + result);
	}
	
	public void findAndUpdate(){
		BasicDBObject updateObject = new BasicDBObject();
		updateObject.put("x", 4.0);
		updateObject.put("j", "tt");
		DBObject object = collection.findAndModify(new BasicDBObject("x", 4.0), updateObject);
		System.out.println(object);
	}
	
	public void remove(){
		System.out.println(collection.remove(new BasicDBObject("j", new BasicDBObject("$gte", 8))).getN());
	}
	
	public static void main(String[] args) {
		SimpleCRUDTest simpleTest = new SimpleCRUDTest();
		System.out.println("添加前");
		simpleTest.queryAll();
		System.out.println("添加开始");
		// simpleTest.simpleAdd();
		// simpleTest.complexAdd();
		// simpleTest.update();
		// simpleTest.remove();
		//simpleTest.findAndUpdate();
		//simpleTest.queryAll();
		System.out.println("添加结束");
	}

java操作mongodb简单示例

public class SimpleTest {
	public static void main(String[] args) {
		Mongo mongo;
		try {
			mongo = new Mongo();
			List<String> dbNames = mongo.getDatabaseNames();
			for(String dbName : dbNames){
				System.out.println(dbName);
			}
			DB db = mongo.getDB("test");
			System.out.println("======================================");
			Set<String> collectionSet = db.getCollectionNames();
			for(String collectionName : collectionSet){
				System.out.println(collectionName);
			}
			DBCollection collection = db.getCollection("things");
			DBCursor cursor = collection.find();
			while(cursor.hasNext()){
				DBObject object = cursor.next();
				System.out.println(object);
			}
			System.out.println("count:" + cursor.count());
			//System.out.println("length: " + cursor.length());
			System.out.println("size: " + cursor.size());
			System.out.println("cursorId: " + cursor.getCursorId());
			System.out.println("json: " + JSON.serialize(cursor));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}