MongoDB的基本使用

最近用到了MongDB,就把所学的记录下。

首先上官网上下载最新的版本http://www.mongodb.org/downloads

解压后配置数据库路径,例如我的在D:\MongoDB,数据库路径是D:\MongoDB\data

在bin文件夹下输入命令mongod --dbpath D:\MongoDB\data,接着再输入mongo看看连接情况,若出现

Mongo Shell version:x.x.x

connecting to: test

则说明配置成功了,接下来我们就可以使用自己的mongdb了。

 

对于数据库的基本使用也就是增删改查了

package edu.scut.italk.mongodb;

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

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.MongoClient;
import com.mongodb.MongoOptions;

import edu.scut.log4j.Logs;

/**
 * mongodb操作的工具类
 *
 */
public class DBUtil {
    private static MongoClient mongoClient = null;
    private static DB db = null;
    private static DBCollection dbCollection = null;
    
    static {
        try {
            mongoClient = new MongoClient("127.0.0.1", 27017);
            db = mongoClient.getDB("test");
            db.createCollection("chatlogs", null);
            dbCollection = db.getCollection("chatlogs");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 设置唯一性
     * 
     * @param field 要设置的field
     */
    public static void setUnique(String field) {
        BasicDBObject basicDBObject = new BasicDBObject();
        basicDBObject.put(field, 1);
        
        dbCollection.ensureIndex(basicDBObject, "unique_key_name", true);
    }
    
    /**
     * 插入数据库
     * 
     * @param doc
     */
    public static void insert(DBObject... doc) {
        dbCollection.insert(doc);
    }
    
    /**
     * 保存修改,如果数据库中不存在则插入,否则覆盖
     * 
     * @param dbCollection
     * @param dbObject 要保存的对象
     * @return
     */
    public static DBObject save(DBObject dbObject) {
        dbCollection.save(dbObject);
        return dbObject;
    }
    
    /**
     * 根据id查找并返回得到的BasicObject对象
     * 
     * @param dbCollection
     * @param id
     * @return
     */
    public static BasicObject findById(String id) {
        BasicObject basicObject = null;
        BasicObject query = new BasicObject();
        query.put("_id", new ObjectId(id));
        DBObject mOb = dbCollection.findOne(query);
        if(null != mOb)
            basicObject = new BasicObject(mOb.toMap());
        return basicObject;
    }
    
    /**
     * 查找children域为null的basicObject对象
     * 
     * @param dbCollection
     * @return 返回一个包含多个basicobject对象的list
     */
    public static List<BasicObject> findNoChildren() {
        BasicObject basicObject = new BasicObject();
        basicObject.setChildren(null);
        DBCursor dbCursor = dbCollection.find(basicObject);
        List<BasicObject> list = new ArrayList<BasicObject>();
        for (DBObject dbObject : dbCursor) {
            list.add(new BasicObject(dbObject.toMap()));
        }
        dbCursor.close();
        
        return list.size() > 0 ? list : null;
    }
    
    /**
     * 根据content查找并返回得到的BasicObject对象
     * 
     * @param dbCollection
     * @param content
     * @return
     */
    public static BasicObject findByContent(String content) {
        BasicObject basicObject = null;
        DBObject mOb = dbCollection.findOne(new BasicObject("content", content));
        if(null != mOb)
            basicObject = new BasicObject(mOb.toMap());
        return basicObject;
    }
    
    /**
     * 遍历全部
     * 
     * @return
     */
    public static DBCursor findAll() {
        return dbCollection.find();
    }
    
    public static void remove(DBObject dbObject) {
        dbCollection.remove(dbObject);
    }
    
    /**
     * 关闭mongoclient
     */
    public static void closeClient() {
        if(null != mongoClient) {
            mongoClient.close();
        }
    }
    
    public static void dropDB() {
        db.dropDatabase();
    }

}

以上基本把我们需要的操作都加载该工具类中,

使用时像这样

        System.out.println("==================findAll============================");
        DBCursor dbCursor = DBUtil.findAll();
        for (DBObject dbObject : dbCursor) {
            System.out.println(dbObject);
        }
        dbCursor.close();
        System.out.println("=====================================================");

 

posted @ 2013-12-04 11:37  kkrimen  阅读(985)  评论(0编辑  收藏  举报