国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

NoSQL學(xué)習(xí)二:MongoDB基本管理命令

2019-11-14 15:47:12
字體:
供稿:網(wǎng)友

一、MongoDB命令幫助

  在安裝MongoDB后,啟動服務(wù)器進(jìn)程(mongod),可以通過在客戶端命令mongo實現(xiàn)對MongoDB的管理和監(jiān)控:

這是MongoDB最上層的命令列表,主要是管理數(shù)據(jù)庫的:數(shù)據(jù)庫操作幫助、集合操作幫助、管理幫助。如果想了解數(shù)據(jù)庫操作更詳細(xì)的命令,可以直接使用db.help(),如下所示:

二、基本命令

  1、show dbs
    顯示當(dāng)前數(shù)據(jù)庫服務(wù)器上的數(shù)據(jù)庫
  2、use pagedb
     切換到指定數(shù)據(jù)庫pagedb的上下文,可以在此上下文中管理pagedb數(shù)據(jù)庫以及其中的集合等
  3、show collections
    顯示數(shù)據(jù)庫中所有的集合(collection)
  4、db.serverStatus()  
    查看數(shù)據(jù)庫服務(wù)器的狀態(tài)。
 
  5、查詢指定數(shù)據(jù)庫統(tǒng)計信息
    use myDatabase
    db.stats()
 
  6、查詢指定數(shù)據(jù)庫包含的集合名稱列表
    
 

三、基本DDL和DML

  3.1 數(shù)據(jù)庫操作

  •   切換、創(chuàng)建數(shù)據(jù)庫 use yourDB
  •   查詢所有數(shù)據(jù)庫 show dbs
  •   刪除當(dāng)前使用數(shù)據(jù)庫 db.dropDatabase()
  •   從指定主機上克隆數(shù)據(jù)庫 db.cloneDatabase("127.0.0.1")
  •   從指定的機器復(fù)制數(shù)據(jù)庫數(shù)據(jù)到某個數(shù)據(jù)庫 db.copyDatabase("mydb","temp","127.0.0.1")
  •   修復(fù)數(shù)據(jù)庫 db.repairDatabase()
  •   查看當(dāng)前使用的數(shù)據(jù)庫 db.getNamedb
  •   顯示當(dāng)前數(shù)據(jù)庫狀態(tài) db.stats()
  •   當(dāng)前db版本 db.version()
  •   查看當(dāng)前數(shù)據(jù)庫機器地址 db.getMongo()  

  3.2 集合操作

  •   創(chuàng)建集合 db.createCollection('collName',{size:20,capped:5,max:100}) 創(chuàng)建成功會顯示{"ok":1}
  •   得到指定名稱的集合 db.getCollection("account")
  •   得到當(dāng)前數(shù)據(jù)庫所有集合 db.getCollectionNames()
  •   得到當(dāng)前數(shù)據(jù)庫所有集合索引的狀態(tài) db.PRintCollectionStats()

  3.3 用戶相關(guān)

  •   添加用戶 db.addUser("name")db.addUser("userName","pwd123",true) 設(shè)置密碼、是否只讀
  •   數(shù)據(jù)庫認(rèn)證 db.auth("userName","123123")
  •   顯示當(dāng)前所用用戶 show users
  •   刪除用戶 db.removeUser("userName")

  3.4 集合查詢

  •   查詢所用記錄 db.userInfo.find() 默認(rèn)每頁顯示20條記錄
  •   查詢?nèi)サ艉蟮漠?dāng)前集合中的某列的重復(fù)數(shù)據(jù) db.userInfo.disinct("name")
  •   等于 db.userInfo.find({"gae":22}) 查詢age==22的集合
  •   大于 db.userInfo.find({"age":{$gt:22}})
  •   小于 db.userInfo.find({"age":{$lt:22}})  
  •   包含 db.userInfo.find({name:/mongo/})
  •   開頭 db.userInfo.find({name:/^mongo/})
  •   查詢指定列name、age db.userInfo.find({},{name:1,age:1})
  •   查詢指定數(shù)據(jù) db.userInfo.find({age:{$gt25}},{name:1,age:1})
  •   升序 db.userInfo.find().sort({age:1})
  •   降序 db.userInfo.find().sort({age:-1})
  •   查詢前5條數(shù)據(jù) db.userInfo.find().limit(5)
  •   查詢10條以后的數(shù)據(jù) db.userInfo.find().skip(10)
  •   查詢5-10條數(shù)據(jù) db.userInfo.find().limit(10).skip(5)
  •   與查詢 db.userInfo.find({$or:[{age:22},{age:25}]})
  •   查詢第一條數(shù)據(jù) db.userInfo.findOne()
  •   查詢集合的條數(shù) db.userInfo.find().count()
  •   按某列進(jìn)行排序 db..find({sex:{$exists:true}}).count()
  •     group  db.userInfo.group({"key": {"age": true},"initial": {"user": []},reduce": function(cur,prev){prev.user.push(cur.name);}
        參數(shù)選項:

             1.key: 這個就是分組的key 2.initial: 每組都分享一個初始化函數(shù),特別注意:是每一組initial函數(shù)。 3.reduce: 這個函數(shù)的第一個參數(shù)是當(dāng)前的文檔對象,第二個參數(shù)是上一次function操作的累計對象。有多少個文檔, $reduce就會調(diào)用多少次。            4.condition: 這個就是過濾條件。 5.finalize: 這是個函數(shù),每一組文檔執(zhí)行完后,多會觸發(fā)此方法。

  3.5 索引

  •   添加索引 db.users.ensureIndex({"name":1}) ensureIndex方法參數(shù)中,數(shù)字1表示升序,-1表示降序。
  •   唯一索引 db.users.ensureIndex({"name":1},{"unique":true})
  •   組合索引 db.users.ensureIndex({"name":1, "age":-1})
  •   查看索引 db.users.getIndexes()
  •   查詢索引 db.users.find({"name":"k"}).hint({"name":1,"age":1})
  •   刪除索引 db.users.dropIndexes()
  •   刪除指定索引 db.users.dropIndex("name_1")

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日土县| 旬阳县| 灯塔市| 大同县| 康马县| 双峰县| 措勤县| 吕梁市| 南宫市| 玉田县| 乌苏市| 丰原市| 白河县| 呈贡县| 交城县| 丹东市| 隆尧县| 左云县| 绵竹市| 静安区| 宁夏| 大渡口区| 陆丰市| 肥东县| 云林县| 杨浦区| 临颍县| 民乐县| 江孜县| 和龙市| 神农架林区| 扶风县| 平阴县| 佛山市| 晋宁县| 乐安县| 白水县| 瓦房店市| 曲靖市| 壤塘县| 石林|