mogodb串連測試,mogodb串連

來源:互聯網
上載者:User

mogodb串連測試,mogodb串連

–$in

db.Student.find({"_id":{"$in":[1001,1002,1]}})

–$or

db.Student.find({"$or":[{"_id":99},{"pageViews":2}]})

–and and or

db.Student.find({"pageViews":2,"$or":[{"_id":99}]})

遊標控制

for(i=0;i<10;i++){    db.Student.insert({"_id":i});}var cursor=db.Student.find();cursor.forEach(function(x){    print(x);});

–分頁查詢

db.Student.find().limit(3)     db.Student.find().limit(3).skip(3)

–排序

db.Student.find().sort({"_id":-1})      --1為升序,-1為降序db.Student.find().sort({"_id":-1,"pageView":1})  --前面如果相同就按後面pageView的升序來排序

DBHelper:

package com.yc.link;import java.net.UnknownHostException;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.Mongo;import com.mongodb.MongoException;public class DBHelper {        private static Mongo mongo=null;        private DB db=null;        static{            try {                mongo=new Mongo(Mypro.getInstance().getProperty("ip"),Integer.parseInt(Mypro.getInstance().getProperty("port")));            } catch (NumberFormatException e) {                e.printStackTrace();            } catch (UnknownHostException e) {                e.printStackTrace();            } catch (MongoException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        public boolean getDB(String dbName,String name,String pwd){            mongo.getDB(dbName);            if(name!=null&&!"".equals(name)&&pwd!=null&&!"".equals(pwd)){                if(db.authenticate(name, pwd.toCharArray())){                    return true;                }else{                    return false;                }            }else{//查看設定檔中是否給定使用者名稱和密碼                String uname=Mypro.getInstance().getProperty("uname");                String pwds=Mypro.getInstance().getProperty("password");                if(name!=null&&!"".equals(name)&&pwd!=null&&!"".equals(pwd)){                    if(db.authenticate(name, pwd.toCharArray())){                    return true;                }else{                    return false;                }            }        }        return true;}        /**         * 關閉連結         * */public void closeAll(Mongo mongo){    if(mongo!=null){        mongo.close();    }}/** * 擷取指定的集合 * collectionName要連結的集合 * dbName集合所在的資料庫 * 連結資料的使用者名稱 * 連結資料庫的密碼 *  * */public DBCollection getDBCollection(String collectionName,String dbName){    DBCollection dbCollection=null;    if(getDB(dbName,null,null)){        db.requestStart();        if(collectionName==null){//如果為空白則沖設定檔中載入            collectionName=Mypro.getInstance().getProperty(collectionName);        }        dbCollection= db.getCollection(collectionName);    }else{        throw new RuntimeException("資料庫連接失敗.....");    }    return dbCollection;}public DBCollection getDBCollection(String collectionName,String dbName,String name,String password){    DBCollection dbCollection=null;    if(getDB(dbName,name,password)){        db.requestStart();        if(collectionName==null){//如果為空白則沖設定檔中載入            collectionName=Mypro.getInstance().getProperty(collectionName);        }        dbCollection= db.getCollection(collectionName);    }else{        throw new RuntimeException("資料庫連接失敗.....");    }    return dbCollection;}}

Mypro:單例

package com.yc.link;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class Mypro extends Properties {        private static Mypro instance=new Mypro();        private Mypro(){            InputStream is=Mypro.class.getResourceAsStream("/db.properties");            try {                this.load(is);            } catch (IOException e) {                e.printStackTrace();            }finally{                if(is!=null){                    try {                        is.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }        }        public static Mypro getInstance(){            return instance;        }}
package com.yc.link;public class Test {    public static void main(String[] args) {            DBHelper db=new DBHelper();            System.out.println(db);    }}

db.properties

ip=127.0.0.1port=27017dbName=uname=password=collectionName=

DBHelper裡應該要多封裝一些常用方法,大家可以到網上慢慢收集

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.