> 数据库 > MongoDB >

MongoDB 之Java应用测试

不多说了,看源码

package corejava2.mongo.basic;

import java.net.UnknownHostException;

import org.bson.types.ObjectId;

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;

/**
 * TODO
 * 
 * @author Master HaKu
 * @version TODO
 */
public class MongoDbTest {

    private Mongo mg = null;
    private DB db;
    private DBCollection users;

    /**
     * 初始化 TODO
     */
    public void init() {
        try {
            // mg=new Mongo();
            mg = new Mongo("192.168.16.103", 27017);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MongoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 获取temp DB 如果默认没有 会自动创建
        db = mg.getDB("temp");
        // 获取users Collection 如果默认没有 会自动创建
        users = db.getCollection("users");

    }

    /**
     * 销毁 TODO
     */
    public void destory() {
        if (mg != null) {
            mg.close();
        }
        mg = null;
        db = null;
        users = null;
        System.gc();

    }

    /**
     * <b>function:</b> 查询所有数据
     * 
     * @author Master HaKu
     * @createDate 2013-02-07
     */
    public void queryAll() {
        print("查询users的所有数据");
        // Db游标
        DBCursor cur = users.find();
        while (cur.hasNext()) {
            print(cur.next());
        }
    }

    /**
     * 
     * <b>function:</b>添加数据
     * 
     * @author Master HaKu
     * @createDate 2013-02-07
     */
    public void add() {
        queryAll();

        print("count:" + users.count());
        DBObject user = new BasicDBObject();
        user.put("name", "david");
        user.put("age", 30);

        // users.save(user);
        // print(users.save(user).getN());

        user.put("sex", "男");
        print(users.save(user).getN());

        print("count:" + users.count());
        queryAll();

    }

    /**
     * 
     * <b>function:</b>删除数据
     * 
     * @author Master HaKu
     * @createDate 2013-02-07
     */
    public void remove() {
        queryAll();
        print("删除id = 51135e5649a6af891f85f8f3:"
                + users.remove(
                        new BasicDBObject("_id", new ObjectId("51135e5649a6af891f85f8f3")))
                        .getN());
        queryAll();
    }

    public void print(Object o) {
        System.out.println(o);
    }

    public void start() {
        init();
        add();
        //destory();
        remove();
        queryAll();

    }

    /**
     * TODO
     * 
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MongoDbTest t = new MongoDbTest();
        t.start();
    }
}

 

运行结果:

查询users的所有数据
{ "_id" : { "$oid" : "5113608549a6cfc804af1d1b"} , "name" : "david" , "age" : 30 , "sex" : "男"}
{ "_id" : { "$oid" : "511360ad49a60eb5a8479be7"} , "name" : "david" , "age" : 30 , "sex" : "男"}



(责任编辑:IT)