MongoDB基于Java、PHP的一般操作和用戶安全設(shè)置(1)

字號(hào):


    用JAVA語言操作MongoDB
    在官方網(wǎng)站中下載mongo.jar,并添加到項(xiàng)目中。
    創(chuàng)建類MongoDBTest.java
    可以使用如下兩種方式得到數(shù)據(jù)庫連接對(duì)象:
    Mongo m1 = new Mongo();//默認(rèn)本機(jī)連接
    Mongo m2 = new Mongo("localhost", 27017);//連接地址,端口號(hào)
    在創(chuàng)建連接對(duì)象之后,得到數(shù)據(jù)庫:
    DB db = m.getDB("admin");//數(shù)據(jù)庫名稱:admin 如果數(shù)據(jù)庫不存在 則自動(dòng)創(chuàng)建 
    在得到數(shù)據(jù)庫對(duì)象之后,得到表:
    DBCollection dbc = db.getCollection("things");//數(shù)據(jù)庫admin下的表things 如沒有此表 則自動(dòng)創(chuàng)建 
    mongoDB基于JAVA語言的CRUD ---
    1.添加數(shù)據(jù):
    DBObject o = new BasicDBObject();//創(chuàng)建一個(gè)對(duì)象
    o.put("name", "iteye");//添加一個(gè)鍵值對(duì)
    o.put("myname", "xiao9");//再添加一個(gè)鍵值對(duì)
    dbc.insert(o);//插入數(shù)據(jù)
    2.查詢數(shù)據(jù)
    DBCursor c = dbc.find();//查詢所有列表
    List<DBObject> list = c.toArray();
    for (int i = 0; i <list.size(); i++) {
    DBObject dbo = list.get(i);
    System.out.println(dbo.toString());
    }
    DBObject o = new BasicDBObject();
    o.put("name", "iteye");
    DBCursor c = dbc.find(o);//根據(jù)條件查詢列表 (name=iteye)
    DBObject o = dbc.findOne();//查詢第一個(gè)數(shù)據(jù)
    DBObject o = new BasicDBObject();
    o.put("name", "iteye");
    DBObject o = dbc.findOne(o);//根據(jù)條件查詢單個(gè)數(shù)據(jù)
    3.修改數(shù)據(jù)
    DBObject queryObject = new BasicDBObject();
    queryObject.put("name", "iteye");
    DBObject obj = new BasicDBObject();
    queryObject.put("name", "iteye123");
    dbc.update(queryObject, obj);//查詢條件,要修改的值
    4.刪除數(shù)據(jù)
    DBObject obj = new BasicDBObject();
    queryObject.put("name", "iteye123");
    dbc.remove(obj);//根據(jù)條件刪除數(shù)據(jù)
    用PHP語言操作MongoDB
    <?php
    //得到MongoDB連接
    $m = new Mongo();
    //選擇數(shù)據(jù)庫comedy
    $db = $m->comedy;
    //選擇一個(gè)表 如沒有此表則自動(dòng)創(chuàng)建
    $collection = $db->cartoons;
    //創(chuàng)建一個(gè)對(duì)象
    $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
    //插入對(duì)象到數(shù)據(jù)庫
    $collection->insert($obj);
    //創(chuàng)建一個(gè)對(duì)象
    $obj = array( "title" => "XKCD", "online" => true );
    //插入對(duì)象到數(shù)據(jù)庫
    $collection->insert($obj);
    //查詢所有該表中的對(duì)象
    $cursor = $collection->find();
    //進(jìn)行遍歷和輸出
    foreach ($cursor as $obj) {
    echo $obj["title"] . "\n";
    }
    //PHP也支持這種得到單個(gè)對(duì)象的API
    $obj = $collection->findOne();
    var_dump( $obj );
    //也可以進(jìn)行循環(huán)插入
    for($i=0; $i<100; $i++) {
    $collection->insert( array( "i" => $i ) );
    }
    //輸出表中所有數(shù)據(jù)的數(shù)量
    echo $collection->count();
    //PHP的條件查詢
    $query = array( "i" => 71 );
    $cursor = $collection->find( $query );
    while( $cursor->hasNext() ) {
    var_dump( $cursor->getNext() );
    }
    //索引的建立
    $coll->ensureIndex( array( "i" => 1 ) ); // create index on "i"
    $coll->ensureIndex( array( "i" => -1, "j" => 1 ) ); // index on "i" descending, "j" ascending
    ?>
    對(duì)于MongoDB的安全設(shè)置,用戶密碼策略
    MongoDB默認(rèn)是不要求用戶名和密碼登陸的,這樣并不安全,接下來就要設(shè)置登陸賬號(hào)密碼了。
    (1)控制臺(tái)設(shè)置用戶密碼和控制臺(tái)通過用戶密碼訪問MongoDB
    1. 啟動(dòng)MongoDB服務(wù)器
    cd d:
    cd mongodb\bin
    mongod --dbpath data
    2. 打開一個(gè)新的CMD運(yùn)行
    cd d:
    cd mongodb\bin
    //打開mongodb數(shù)據(jù)庫操作
    mongo.exe
    //使用admin庫
    use admin;
    //添加登陸賬號(hào):user1 密碼pwd1
    db.addUser('user1','pwd1');
    //查看是否設(shè)置成功
    //db.system.users.find();
    3. 關(guān)閉MongoDB服務(wù)器,并使用驗(yàn)證模式 ( auth )重新啟動(dòng)
    cd d:
    cd mongodb\bin
    mongod --dbpath data --auth
    接下來在通過CMD運(yùn)行Mongodb的時(shí)候 就需要
    cd d:
    cd mongodb\bin
    mongo.exe
    use admin;
    //進(jìn)行登陸驗(yàn)證,如果不通過,是沒有操作權(quán)限的了。
    db.auth('user1','pwd1');
    (2)JAVA方式通過用戶密碼訪問MongoDB
    Mongo m = new Mongo();
    DB db = m.getDB("admin");
    char[] pwd_char = "pwd1".toCharArray();
    boolean auth = db.authenticate("user1",pwd_char);//登陸驗(yàn)證,成功之后才能進(jìn)行有效操作
    if(!auth){
    throw new RuntimeException();
    }
    (3)PHP方式通過用戶密碼訪問MongoDB
    //PHP是直接在獲取連接對(duì)象時(shí)就進(jìn)行配置了
    //mongodb://賬號(hào):密碼@連接地址
    $m = new Mongo("mongodb://user1:pwd1@localhost");