Java连接MongoDB代码示例

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

连接, 获取db, collection, 增加, 删除, 更新, 查询等基本操作

import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.DBTCPConnector;
import com.mongodb.Mongo;
import com.mongodb.WriteResult;

/**
 * 对于mongon的测试
 * 
 * @author Gaojie
 */
public class MongoDBTest {

    Mongo m;
    DBTCPConnector conn;
    private DB db;

    @Test
    public void testMongo() throws UnknownHostException {
        String host = "202.108.*.*";
        m = new Mongo(host, 27017);
        // m.get
    }

    @Test
    public void testConntect() throws Exception {
        testMongo();
        System.out.println("Mongo: " + m);
        conn = m.getConnector();
        System.out.println("DBTCPConnector: " + conn.getServerAddressList());
    }

    @Test
    public void testDB() throws Exception {
        testMongo();
        testConntect();

        // 获取admin的数据库
        db = m.getDB("gaojie");
        System.out.println("DB=" + db);
        boolean auth = db.authenticate("root", "123456".toCharArray());
        System.out.println("auth=" + auth);
    }

    @Test
    public void testGetAll() throws Exception {
        testDB();
        // 获取db里面的collection(表)
        Set<String> names = db.getCollectionNames();

        for (String name : names) {
            System.out.println("CollectionName: " + name);
            DBCollection coll = db.getCollection(name);
            System.out.println("CollectionCount=" + coll.count());

            DBCursor cursor = coll.find();
            while (cursor.hasNext()) {
                System.out.println("DBObject=" + cursor.next());
            }

            List<DBObject> objs = coll.getIndexInfo();
            for (DBObject obj : objs) {
                System.out.println("IndexInfo=" + obj);
            }
            System.out.println("==============");
        }
        // 获取表结果
    }

    @Test
    public void testCollection() throws Exception {
        testDB();

        if (db.isAuthenticated()) {
            for (String coll : db.getCollectionNames()) {
                System.out.println("collection=" + coll);
            }
        }
    }

    @Test
    public void testInsert() throws Exception {

        testDB();

        if (db.isAuthenticated()) {
            DBCollection coll = db.getCollection("test");
            DBObject obj = new BasicDBObject();
            obj.put("name", "gaojie");
            obj.put("age", 2403);
            obj.put("md5", "546466sfsddfsd");
            WriteResult wr = coll.insert(obj);
            System.out.println("WriteResult=" + wr);
        }
    }

    @Test
    public void testInsertObj() throws Exception {

        testDB();
        if (db.isAuthenticated()) {
            DBCollection coll = db.getCollection("test");
            DBObject obj = new BasicDBObject();
            obj.put("pepole", new User());

            WriteResult wr = coll.insert(obj);
            System.out.println("WriteResult=" + wr);
        }
    }

    @Test
    public void testRemove() throws Exception {
        testDB();
        if (db.isAuthenticated()) {
            DBCollection coll = db.getCollection("test");
            DBObject obj = new BasicDBObject();
            // obj.put("name", "gaojie1");
            obj.put("age", 110);
            System.out.println("WriteResult=" + coll.remove(obj));
        }
    }

    @Test
    public void testUpdate() throws Exception {
        testDB();

        if (db.isAuthenticated()) {
            DBCollection coll = db.getCollection("test");
            DBObject obj = new BasicDBObject();
            obj.put("name", "gaojie10");
            obj.put("age", 110);

            DBObject upObj = new BasicDBObject();
            upObj.put("name", "gaojie10");

            // upObj.put("age", 110);

            System.out.println("WriteResult=" + coll.update(obj, upObj));
        }
    }

    @Test
    public void testQuery() throws Exception {
        testDB();

        if (db.isAuthenticated()) {
            DBCollection coll = db.getCollection("test");
            DBObject obj = new BasicDBObject();
            // obj.put("name", "gaojie00");
            obj.put("age", 110);

            DBCursor cursor = coll.find(obj);
            while (cursor.hasNext()) {
                System.out.println("DBObject=" + cursor.next());
            }
        }
    }
}

标签: ssl 数据库

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:获取android手机中已安装应用信息以及应用安装和卸载代码集合

下一篇: Android获取App版本号和版本名