欢迎您光临本小站。希望您在这里可以找到自己想要的信息。。。

Java操作MongoDB的增删改查代码

nosql water 1270℃ 0评论

java操作mysql数据库的代码我们已经了如指掌了,增删改查,java对mongodb数据库也是类似的操作,先是数据库连接,再是进行操作。

 
 
首先我们进入进入admin数据库,然后建立自己的数据库testMongoDb,进入admin数据库后,就可以直接进入testMongoDb,因为
用户可以进入系统的数据库,就是超级管理员,use
testMongoDb后,为该数据库设置用户名和密码,db.addUser('root','root'),这样我们在程序中连该数据库,并实现增删
改查,代码如下所示。

    代码如下所示:

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

package com.zhongsou.mongo;

 
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());
            }
        }
    }
}

转载请注明:学时网 » Java操作MongoDB的增删改查代码

喜欢 (0)or分享 (0)

您必须 登录 才能发表评论!