MongoDB常用命令小結_MongoDB

來源:互聯網
上載者:User

MongoDB常用命令:

超級使用者相關:

use admin
#增加或修改使用者密碼
db.addUser(ixigua,'pwd')
#查看使用者列表
db.system.users.find()
#使用者認證
db.auth(ixigua,'pwd')
#刪除使用者
db.removeUser('mongodb')
#查看所有使用者
show users
#查看所有資料庫
show dbs
#查看所有的collection
show collections
#查看各collection的狀態
db.printCollectionStats()
#查看主從複製狀態
db.printReplicationInfo()
#修複資料庫
db.repairDatabase()
#設定記錄profiling,0=off 1=slow 2=all
db.setProfilingLevel(1)
#查看profiling
show profile
#拷貝資料庫
db.copyDatabase('mail_addr','mail_addr_tmp')
#刪除collection
db.mail_addr.drop()
#刪除當前的資料庫
db.dropDatabase()


用戶端串連:
/usr/local/mongodb/bin/mongo 8.8.88/ixigualib -u ixigua -p 'pwd'


增刪改:
#儲存嵌套的對象
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
#儲存數組對象
db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
#根據query條件修改,如果不存在則插入,允許修改多條記錄
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
#刪除yy=5的記錄
db.foo.remove({'yy':5})
#刪除所有的記錄
db.foo.remove()


索引:
#增加索引:1(ascending),-1(descending)
db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
#索引子物件
db.user_addr.ensureIndex({'Al.Em': 1})
#查看索引資訊
db.deliver_status.getIndexes()
db.deliver_status.getIndexKeys()
#根據索引名刪除索引
db.user_addr.dropIndex('Al.Em_1')


查詢:
#尋找所有
db.foo.find()
#尋找一條記錄
db.foo.findOne()
#根據條件檢索10條記錄
db.foo.find({'msg':'Hello 1'}).limit(10)
#sort排序
db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1})
db.deliver_status.find().sort({'Ct':-1}).limit(1)
#count操作
db.user_addr.count()
#distinct操作
db.foo.distinct('msg')
#>操作
db.foo.find({"timestamp": {"$gte" : 2}})
#子物件的尋找
db.foo.find({'address.city':'beijing'})


管理:
#查看collection資料的大小
db.deliver_status.dataSize()
#查看colleciont狀態
db.deliver_status.stats()
#查詢所有索引的大小
db.deliver_status.totalIndexSize()
#查看當前所使用的資料庫
db

作者 mahout

以下是其他網友的補充:

庫操作
show dbs 查看系統中的資料庫列表
註:資料庫中必須有內容才會顯示
db = db.getSiblingDB("<資料庫名>")
or
use <資料庫名> 
切換當前資料庫
註:如果資料庫不存在,則建立資料庫
任何一種方法都是可以的,並把db值設定為指定的資料庫,然後可以使用db來管理新的當前資料庫。
 
db.dropDatabase() 刪除當前資料庫
註:刪除當前資料庫,然後在不改變當前資料庫的情況下使用控制代碼建立一個集合,則被刪的資料庫會被重新建立。
 
db.copyDatabase("<當前資料庫名>", "<備份庫名>", [hostname])
複製資料庫,建立除名稱外一模一樣的資料庫
註:可選的hostname參數指定當前庫MongDB伺服器主機名稱。
 
db 顯示當前資料庫

集合操作
show collections 查看當前資料庫中包含的集合的名單
 
db.createCollection("<集合名>", {<options>}) 建立集合
註:參數二可選對象
屬性          說明
capped         布爾,如果為true,表示該集合是一個封頂集合,它不會增長到比size屬性指定最大規模更大。預設:false
autoIndexID     布爾,如果為true,表明自動為添加到集合的每個文檔建立一個_id欄位並實現該欄位上索引。這對封頂集合應該是false。預設true
size          位元組單位大小,用於封頂集合。最舊的檔案被刪除,以騰出空間給新檔案
max         在封頂集合中允許的最大文檔數。最舊的檔案被刪除,以騰出空間給新檔案

db.<集合名>.drop()
or
coll = db.getCollection("<集合名>")
coll.drop() 刪除集合
 
db.<集合名>.find()
or
coll = db.getCollection("<集合名>")
coll.find(query) 在集合中查看文檔
註:可選query參數指定包含欄位和值的查詢文檔與集合中的文檔匹配,並返回匹配內容。例:coll.find({speed:"120mph"})
 
db.<集合名>.insert({}) 
or
db.<集合名>.save({})
在集合中插入文檔資料(如果集合不存在會被建立,save也是)
將同質資料視為一個集合
集合中資料具體相似的屬性
集合是一個容器,內部可放置N多資料
可以對這些資料進行篩選、排序
db.<集合名>.remove({篩選條件},false)
從集合中刪除文檔 參數1:篩選條件。參數2:選擇性參數,是否單行刪除,預設false。
 
db.<集合名>.save({ "_id" : ObjectId("57e26b294a655f35e13d6f5d"), "name" : "hung", "age" : 18})
or
db.<集合名>.update( {篩選條件}, {$set:{"name":"jin","up":true}},
{upsert:true,multi:true}) 更新集合中的文檔
參數一:更新文檔的條件
參數二:更新時使用的更新運算子,以及更新屬性
常用運算子:$inc遞迴該欄位值。$set設定欄位值。$push將一個條目推送數組。$rename重新命名欄位。等··
參數三:可選,兩個屬性。multi(匹配到的所有文檔更新,預設false只更新第一條)和upsert(若沒有找到匹配項,建立一個,預設false不建立),布爾。
*******************************************************************
db.<集合名>.pretty() 整齊的顯示查詢文檔
db.<集合名>.find().sort({屬性:1/-1}) 按指定的屬性排序
1是正序,-1:倒序
db.<集合名>.find().limit(n) 顯示前n條資料
db.<集合名>.find().skip(n) 跳過前n條資料
條件尋找
db.<集合名>.find({屬性:value}) 屬性==值
db.<集合名>.find({屬性:{運算子:value}})
運算子:
$lt       小於
$lte     小於等於
$gt      大於
$gte    大於等於
$ne     不等於
對同一屬性使用兩個運算子
db.<集合名>.find({ 屬性: { 運算子1:value, 運算子2:value } })
如:db.<集合名>.find({ age: { $gt: 6, $lt: 16 } })
對不同屬性同時限定條件(兩個條件同時成立)
db.<集合名>.find( {屬性1: { 運算子:value }, 屬性2: { 運算子:value }})
或條件
db.<集合名>.find({
$or: [
{屬性: value},
{屬性: value},
{屬性: value}
]
})
使用函數篩選資料
db.<集合名>.find({
$where: function(){
return true/false
}
})

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.