Java连接MongoDB代码示例详解编程语言

连接, 获取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()); 
            } 
        } 
    } 
} 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/11006.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论