欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MongoDB系列教程(六):java操作mongodb實(shí)例

 更新時(shí)間:2015年05月07日 11:27:17   投稿:junjie  
這篇文章主要介紹了MongoDB系列教程(六):java操作mongodb實(shí)例,本文講解了java中操作mongodb數(shù)據(jù)增加、刪除、修改、查詢數(shù)據(jù)等代碼實(shí)例,需要的朋友可以參考下

java操作mysql數(shù)據(jù)庫的代碼我們已經(jīng)了如指掌了,增刪改查,java對(duì)mongodb數(shù)據(jù)庫也是類似的操作,先是數(shù)據(jù)庫連接,再是進(jìn)行操作。

首先我們進(jìn)入進(jìn)入admin數(shù)據(jù)庫,然后建立自己的數(shù)據(jù)庫testMongoDb,進(jìn)入admin數(shù)據(jù)庫后,就可以直接進(jìn)入testMongoDb,因?yàn)橛脩艨梢赃M(jìn)入系統(tǒng)的數(shù)據(jù)庫,就是超級(jí)管理員,use testMongoDb后,為該數(shù)據(jù)庫設(shè)置用戶名和密碼,db.addUser('root','root'),這樣我們?cè)诔绦蛑羞B該數(shù)據(jù)庫,并實(shí)現(xiàn)增刪改查,代碼如下所示。

代碼如下所示:

復(fù)制代碼 代碼如下:

package com.mkyong.core; 
 
import java.net.UnknownHostException; 
import java.util.Date; 
import com.mongodb.BasicDBObject; 
import com.mongodb.DB; 
import com.mongodb.DBCollection; 
import com.mongodb.DBCursor; 
import com.mongodb.Mongo; 
import com.mongodb.MongoException; 
 
/**
 * Java + MongoDB Hello world Example
 * 
 */ 
public class App { 
    public static void main(String[] args) { 
 
        try { 
 
            /**** Connect to MongoDB ****/ 
            // Since 2.10.0, uses MongoClient 
            //MongoClient mongo = new MongoClient("localhost", 27017); 
             
            Mongo mongo = new Mongo("127.0.0.1",27017); 
             
           
            /**** Get database ****/ 
            // if database doesn't exists, MongoDB will create it for you 
            DB db = mongo.getDB("testMongoDb"); 
            //database username  root  and password root  
            boolean ok = db.authenticate("root","root".toCharArray()); 
            if(ok){ 
                System.out.println("db connection success!"); 
                 
            }{ 
                System.out.println("db connection fail !"); 
            } 
            /**** Get collection / table from 'testMongoDb' ****/ 
            // if collection doesn't exists, MongoDB will create it for you 
            DBCollection table = db.getCollection("user"); 
 
            /**** Insert ****/ 
            // create a document to store key and value 
            BasicDBObject document = new BasicDBObject(); 
            document.put("name", "mkyong"); 
            document.put("age", 30); 
            document.put("createdDate", new Date()); 
            table.insert(document); 
 
            /**** Find and display ****/ 
            BasicDBObject searchQuery = new BasicDBObject(); 
            searchQuery.put("name", "mkyong"); 
 
            DBCursor cursor = table.find(searchQuery); 
 
            while (cursor.hasNext()) { 
                System.out.println(cursor.next()); 
            } 
 
            /**** Update ****/ 
            // search document where name="mkyong" and update it with new values 
            BasicDBObject query = new BasicDBObject(); 
            query.put("name", "mkyong"); 
 
            BasicDBObject newDocument = new BasicDBObject(); 
            newDocument.put("name", "mkyong-updated"); 
 
            BasicDBObject updateObj = new BasicDBObject(); 
            updateObj.put("$set", newDocument); 
 
            table.update(query, updateObj); 
 
            /**** Find and display ****/ 
            BasicDBObject searchQuery2  
                = new BasicDBObject().append("name", "mkyong-updated"); 
 
            DBCursor cursor2 = table.find(searchQuery2); 
 
            while (cursor2.hasNext()) { 
                System.out.println(cursor2.next()); 
            } 
 
            /**** Done ****/ 
            System.out.println("Done"); 
 
        } catch (UnknownHostException e) { 
            e.printStackTrace(); 
        } catch (MongoException e) { 
            e.printStackTrace(); 
        } 
 
    } 

控制臺(tái)輸入結(jié)果如下:

復(fù)制代碼 代碼如下:

    db connection success!
    db connection fail !
{ "_id" : { "$oid" : "544073c4d58dfa6e469555ba"} , "name" : "mkyong" , "age" : 30 , "createdDate" : { "$date" : "2014-10-17T01:41:24.479Z"}}
{ "_id" : { "$oid" : "543e154bd58d704982fd38f0"} , "name" : "mkyong-updated" , "age" : 30 , "createdDate" : { "$date" : "2014-10-15T06:33:47.321Z"}}
{ "_id" : { "$oid" : "5440719dd58d08a207605c8e"} , "name" : "mkyong-updated" , "age" : 30 , "createdDate" : { "$date" : "2014-10-17T01:32:13.922Z"}}
{ "_id" : { "$oid" : "544073c4d58dfa6e469555ba"} , "name" : "mkyong-updated" , "age" : 30 , "createdDate" : { "$date" : "2014-10-17T01:41:24.479Z"}}
Done


源碼下載:http://xiazai.jb51.net/201503/other/mongodb_helloworld.zip

相關(guān)文章

  • MongoDB中文檔的更新操作示例詳解

    MongoDB中文檔的更新操作示例詳解

    這篇文章主要給大家介紹了關(guān)于MongoDB中文檔的更新操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Pycharm連接MongoDB數(shù)據(jù)庫安裝教程詳解

    Pycharm連接MongoDB數(shù)據(jù)庫安裝教程詳解

    這篇文章主要介紹了Pycharm連接MongoDB數(shù)據(jù)庫安裝教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • MongoDB系列教程(四):設(shè)置用戶訪問權(quán)限

    MongoDB系列教程(四):設(shè)置用戶訪問權(quán)限

    這篇文章主要介紹了MongoDB系列教程(四):設(shè)置用戶訪問權(quán)限,本文講解了在Windows環(huán)境下如何創(chuàng)建用戶認(rèn)證,需要的朋友可以參考下
    2015-05-05
  • MongoDB數(shù)據(jù)庫的日志文件深入分析

    MongoDB數(shù)據(jù)庫的日志文件深入分析

    這篇文章主要給大家介紹了關(guān)于MongoDB數(shù)據(jù)庫日志的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MongoDB具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Mongodb 刪除添加分片與非分片表維護(hù)

    Mongodb 刪除添加分片與非分片表維護(hù)

    MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。通過本文給大家介紹Mongodb 刪除添加分片與非分片表維護(hù)的相關(guān)知識(shí),對(duì)此文感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • 記一次MongoDB性能問題(從MySQL遷移到MongoDB)

    記一次MongoDB性能問題(從MySQL遷移到MongoDB)

    最近忙著把一個(gè)項(xiàng)目從MySQL遷移到MongoDB,在導(dǎo)入舊數(shù)據(jù)的過程中,遇到了些許波折,犯了不少錯(cuò)誤,但同時(shí)也學(xué)到了不少知識(shí),遂記錄下來,需要的朋友可以參考下
    2017-03-03
  • 詳解mongodb 主從配置

    詳解mongodb 主從配置

    我研究過的nosql,memcache,redis,mongodb都是支持分布式的,生產(chǎn)環(huán)境中用過memcache,redis,性能穩(wěn)定。mongodb是最接近關(guān)系型數(shù)據(jù)庫的,不用花很多時(shí)間去構(gòu)建數(shù)據(jù)庫模型,將來我會(huì)用mongodb,看一下主從配置
    2014-07-07
  • MongoDB如何正確中斷正在創(chuàng)建的索引詳解

    MongoDB如何正確中斷正在創(chuàng)建的索引詳解

    這篇文章主要給大家介紹了關(guān)于MongoDB如何正確中斷正在創(chuàng)建的索引的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • MongoDB安裝圖文教程

    MongoDB安裝圖文教程

    這篇文章主要為大家詳細(xì)介紹了MongoDB安裝圖文教程,分為兩大部分為大家介紹下載MongoDB和安裝MongoDB的方法,感興趣的小伙伴們可以參考一下
    2016-07-07
  • MongoDB時(shí)間戳轉(zhuǎn)日期及日期分組實(shí)例代碼

    MongoDB時(shí)間戳轉(zhuǎn)日期及日期分組實(shí)例代碼

    時(shí)間戳(timestamp)通常是一個(gè)字符序列,唯一地標(biāo)識(shí)某一刻的時(shí)間,下面這篇文章主要給大家介紹了關(guān)于MongoDB時(shí)間戳轉(zhuǎn)日期及日期分組的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05

最新評(píng)論