mongodb Java(八)

package com.mongodb.text;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

public class DataBase {
    public static void main(String[] args) 
        throws UnknownHostException, MongoException {
        //1.創建一個Mongo的數據庫鏈接對象
        Mongo mg = new Mongo("127.0.0.1:27017");
        //查詢全部的Database
        for (String name : mg.getDatabaseNames()) {
            System.out.println("dbName: " + name);
        }
        //2.建立相關數據庫的鏈接
        DB db = mg.getDB("foobar");
        //查詢數據庫全部的集合
        for (String name : db.getCollectionNames()) {
            System.out.println("collectionName: " + name);
        }
        
        DBCollection users = db.getCollection("persons");
        //查詢全部的數據
        DBCursor cur = users.find();
        while (cur.hasNext()) {
            DBObject object = cur.next();
            System.out.println(object.get("name"));
        }
        System.out.println(cur.count());
        System.out.println(cur.getCursorId());
        System.out.println(JSON.serialize(cur));
    }
}
DataBase.java
package com.mongodb.text;

import java.net.UnknownHostException;
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.Mongo;
import com.mongodb.MongoException;

public class MongoDb {
    //1.創建一個Mongo的數據庫鏈接對象
    static Mongo connection = null;
    //2.建立相關數據庫的鏈接
    static DB db = null;
    public MongoDb(String dbName) throws UnknownHostException, MongoException{
        connection = new Mongo("127.0.0.1:27017");
        db = connection.getDB(dbName);
    }
    public static void main(String[] args) throws UnknownHostException, MongoException {
        //實例化
        MongoDb mongoDb = new MongoDb("foobar");
        /**
         * 1.建立一個名字叫javadb的數據庫
         */
//        mongoDb.createCollection("javadb");
        /**
         * 2.爲集合javadb添加一條數據
         */
//        DBObject dbs = new BasicDBObject();
//        dbs.put("name", "uspcat.com");
//        dbs.put("age", 2);
//        List<String>  books = new ArrayList<String>();
//        books.add("EXTJS");
//        books.add("MONGODB");
//        dbs.put("books", books);
//        mongoDb.insert(dbs, "javadb");
        /**
         * 3.批量插入數據
         */
//        List<DBObject> dbObjects = new ArrayList<DBObject>();
//        DBObject jim = new BasicDBObject("name","jim");
//        DBObject lisi = new BasicDBObject("name","lisi");
//        dbObjects.add(jim);
//        dbObjects.add(lisi);
//        mongoDb.insertBatch(dbObjects, "javadb");
        /**
         * 4.根據ID刪除數據
         */
//        mongoDb.deleteById("502870dab9c368bf5b151a04", "javadb");
        /**
         * 5.根據條件刪除數據
         */
//        DBObject lisi = new BasicDBObject();
//        lisi.put("name", "lisi");
//        int count = mongoDb.deleteByDbs(lisi, "javadb");
//        System.out.println("刪除數據的條數是: "+count);
        /**
         * 6.更新操做,爲集合增長email屬性
         */
//        DBObject update = new BasicDBObject();
//        update.put("$set", 
//                new BasicDBObject("eamil","uspcat@126.com"));
//        mongoDb.update(new BasicDBObject(),
//                update,false,true,"javadb");
        /**
         * 7.查詢出persons集合中的name和age
         */
//        DBObject keys = new BasicDBObject();
//        keys.put("_id", false);
//        keys.put("name", true);
//        keys.put("age", true);
//        DBCursor cursor = mongoDb.find(null, keys, "persons");
//        while (cursor.hasNext()) {
//            DBObject object = cursor.next();
//            System.out.println(object.get("name"));
//        }
        /**
         * 7.查詢出年齡大於26歲而且英語成績小於80分
         */
//        DBObject ref = new BasicDBObject();
//        ref.put("age", new BasicDBObject("$gte",26));
//        ref.put("e", new BasicDBObject("$lte",80));
//        DBCursor cursor = mongoDb.find(ref, null, "persons");
//        while (cursor.hasNext()) {
//            DBObject object = cursor.next();
//            System.out.print(object.get("name")+"-->");
//            System.out.print(object.get("age")+"-->");
//            System.out.println(object.get("e"));
//        }
        /**
         * 8.分頁例子
         */
        DBCursor cursor = mongoDb.find(null, null, 0, 3, "persons");
        while (cursor.hasNext()) {
            DBObject object = cursor.next();
            System.out.print(object.get("name")+"-->");
            System.out.print(object.get("age")+"-->");
            System.out.println(object.get("e"));
        }        
        //關閉鏈接對象
        connection.close();
    }
    /**
     * 穿件一個數據庫集合
     * @param collName 集合名稱
     * @param db  數據庫實例
     */
    public void createCollection(String collName){
        DBObject dbs = new BasicDBObject();
        db.createCollection("javadb", dbs);
    }
    /**
     * 爲相應的集合添加數據
     * @param dbs
     * @param collName
     */
    public void insert(DBObject dbs,String collName){
        //1.獲得集合
        DBCollection coll = db.getCollection(collName);
        //2.插入操做
        coll.insert(dbs);
    }
    /**
     * 爲集合批量插入數據
     * @param dbses
     * @param collName
     */
    public void insertBatch(List<DBObject> dbses,String collName){
        //1.獲得集合
        DBCollection coll = db.getCollection(collName);
        //2.插入操做
        coll.insert(dbses);
    }
    /**
     * 根據id刪除數據
     * @param id
     * @param collName
     * @return 返回影響的數據條數
     */
    public int deleteById(String id,String collName){
        //1.獲得集合
        DBCollection coll = db.getCollection(collName);
        DBObject dbs = new BasicDBObject("_id", new ObjectId(id));
        int count = coll.remove(dbs).getN();
        return count;
    }
    /**
     * 根據條件刪除數據
     * @param id
     * @param collName
     * @return 返回影響的數據條數
     */    
    public int deleteByDbs(DBObject dbs,String collName){
        //1.獲得集合
        DBCollection coll = db.getCollection(collName);
        int count = coll.remove(dbs).getN();
        return count;
    }
    /**
     * 更新數據
     * @param find 查詢器
     * @param update 更新器
     * @param upsert 更新或插入
     * @param multi 是否批量更新
     * @param collName 集合名稱
     * @return 返回影響的數據條數
     */
    public int update(DBObject find,
                        DBObject update,
                        boolean upsert,
                        boolean multi,
                        String collName){
        //1.獲得集合
        DBCollection coll = db.getCollection(collName);
        int count = coll.update(find, update, upsert, multi).getN();
        return count;
    }
    /**
     * 查詢器(分頁)
     * @param ref
     * @param keys
     * @param start
     * @param limit
     * @return
     */
    public DBCursor find(DBObject ref, 
            DBObject keys,
            int start,
            int limit,
            String collName){
        DBCursor cur = find(ref, keys, collName);
        return cur.limit(limit).skip(start);
    }
    /**
     * 查詢器(不分頁)
     * @param ref
     * @param keys
     * @param start
     * @param limit
     * @param collName
     * @return
     */
    public DBCursor find(DBObject ref,
            DBObject keys,
            String collName){
        //1.獲得集合
        DBCollection coll = db.getCollection(collName);
        DBCursor cur = coll.find(ref, keys);
        return cur;
    }    
}
MongoDb.java

 一、下載mongodb對Java的驅動java

相關文章
相關標籤/搜索