MongoDb基本增刪改查操作總結

MongoDb基本增刪改查操作總結

前言

之前一直習慣用Mysql數據庫,換了MongoDb的命令行總是不熟練,這裏做一個MongoDb命令行的總結,以後能方便查閱。

 

MongoDb基本使用

1. 數據庫操作

查看數據庫

show dbs 複製代碼

統計數據庫信息

 

 

 

 

use test  # 切換到test數據庫

db.stats()         //統計數據信息
{ 
    "db" : "test",     //數據庫名
    "collections" : 0, //集合數量
    "views" : 0,
    "objects" : 0,     //文檔數量
    "avgObjSize" : 0,  //平均每個文檔的大小
    "dataSize" : 0,    //數據佔用空間大小,不包括索引,單位爲字節
    "storageSize" : 0, //分配的存儲空間
    "nuinExtents" : 0, //連續分配的數據塊
    "indexes" : 0,     //索引個數
    "indexsize" : 0,   //索引佔用空間大小
    "fileSize" : 0,    //物理存儲文件的大小
    "ok" : 1 
}
複製代碼

 

刪除數據庫

db.dropDatabase ()    //刪除當前數據庫,db指向當前使用的test數據庫複製代碼

查看該數據庫下的集合

 

 

 

 

db.getCollectionNames()複製代碼

 

 

2. 集合操作

創建集合

 

 

 

 

db.createCollection(name, options)

eg:db.createCollection("yingSet", {capped:true,size:6142800, max :10000 }) #創建yingSet數據庫複製代碼

                                                options 可以使用的選項

參數 類型 描述
capped Boolean (可選)如果爲 true,則啓用封閉的集合。上限集合是固定大小的集合,它在達到其最大時自動覆蓋其最舊的條目。如果指定 true,則還需要指定 size 參數
size 數字 (可選)指定上限集合的最大大小(以字節爲單位)。如果 capped 爲 true,那麼還需要指定次字段的值
max 數字 (可選)指定上限集合中允許的最大文檔數

如果向一個沒有創建的集合中插入文檔,那麼會先創建這個集合

 

 

 

 

db.yingSet.insert( {"name": "tom"} )    # db.yingSet指向集合對象複製代碼

 

查看該數據庫下的集合

 

 

 

 

show collections
cms_config
cms_page
cms_site
cms_site_server
cms_template
filesystem
fs.chunks
fs.files
sys_dictionary
user_test
複製代碼

重命名集合

 

 

 

 

db.yingSet.renameCollection( "myset")複製代碼

刪除集合

 

 

 

 

db.yingSet.drop()複製代碼

 

3. 文檔操作

插入操作

插入不指定 _id 字段的文檔

db.test.insert( { item : "card", qty : 15 })  #向test集合插入數據複製代碼

插入指定 _id 字段的文檔,值 _id 必須在集合中唯一,以避免重複鍵錯誤

 

 

 

 

 db.test.insert(
    { _id: 10, item: "box", qty: 20 }
) 

複製代碼

用變量方式插入文檔

do = ({ name: "c語言", price: 40 }) 
db.test.insert(do)複製代碼

 

MongoDB 3.2 更新後新增

db.test.insertOne( { item: "card", qty: 15 } );複製代碼

插入的多個文檔

db.test.insertMany([
    { item: "card", qty: 15 },
    { item: "envelope", qty: 20 },
    { item: "stamps", qty:30 }
])複製代碼

 

更新修改操作

obj 代表需要更新的對象,如果集合內部已經存在一個與 obj 相同的“_id”的記錄,Mongodb 會把 obj 對象替換爲集合內已存在的記錄;如果不存在,則會插入 obj 對象。

 

 

 

 

db.collection.save ( obj )

eg:
db.products.save( { _id: 100, item: "watern, qty: 30 })複製代碼

 

刪除操作

db.test.remove({'title': 'MongoDB'})複製代碼

 

 

 

 

db.collection.deleteMany ({})  
db.collection.deleteMany ({ status : "A" })
db.collection.delete.One ({ status : "D" })複製代碼

查詢操作

基本條件查詢

 

 

 

 

db.test.find()
db.test.find().pretty()

複製代碼

                                 MongoDB 與 RDBMS 的查詢比較

操作符 格式 實例 與 RDBMS where 語句比較
等於(=) {<key> : {<value>}} db.test.find( {price : 24} ) where price = 24
大於(>) {<key> : {$gt : <value>}} db.test.find( {price : {$gt : 24}} ) where price > 24
小於(<) {<key> : {$lt : <value>}} db.test.find( {price : {$lt : 24}} ) where price < 24
大於等於(>=) {<key> : {$gte : <value>}} db.test.find( {price : {$gte : 24}} ) where price >= 24
小於等於(<=) {<key> : {$lte : <value>}} db.test.find( {price : {$lte : 24}} ) where price <= 24
不等於(!=) {<key> : {$ne : <value>}} db.test.find( {price : {$ne : 24}} ) where price != 24
與(and) {key01 : value01, key02 : value02, ...} db.test.find( {name : "《》", price : 24} ) where name = "《》" and price = 24
或(or) {$or : [{key01 : value01}, {key02 : value02}, ...]} db.test.find( {$or:[{name : "《》"},{price : 24}]} ) where name = "《》" or price = 24

查詢 age 爲 null 的字段

 

 

 

 

db.test.find({age:null})複製代碼

限制查詢結果的個數

db.test.find().limit(3)複製代碼

用於對查詢結果進行排序,1 是升序,-1 是降序

db.test.find().sort({"price" : 1})複製代碼

使用 $regex 操作符來設置匹配字符串的正則表達式

db.test.find({tags:{$regex:"MongoDB"}})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章