mongodb資料庫的操作語句

2021-09-10 16:05:04 字數 1408 閱讀 3407

,

,inc…)等,也可以理解為sql update查詢內set後面的

upsert : 可選,這個引數的意思是,如果不存在update的記錄,是否插入objnew,true為插入,預設是false,不插入。

multi : 可選,mongodb 預設是false,只更新找到的第一條記錄,如果這個引數為true,就把按條件查出來多條記錄全部更新。

writeconcern :可選,丟擲異常的級別。

例項:通過 update() 方法來更新標題(title):

db.col.update(,})

只更新第一條記錄:

db.col.update( } , } );

全部更新:

db.col.update( } , },false,true );

只新增第一條:

db.col.update( } , },true,false );

全部新增進去:

db.col.update( } , },true,true );

全部更新:

db.col.update( } , },false,true );

只更新第一條記錄:

db.col.update( } , ' at position 22: … }̲,false,false );…or:[,]}).pretty()

且與或聯合使用:

db.col.find(, katex parse error: expected 'eof', got '}' at position 44: … "mongodb 教程"}]}̲).pretty()     …gt : 100}})

取"col"集合中 「likes」 大於等於 100 的資料:

db.col.find(' at position 10: gte : 100}̲})   獲取"col"集合中…lt : 150}})

獲取"col"集合中 「likes」 小於等於 150 的資料:

db.col.find(' at position 10: lte : 150}̲})   獲取"col"集合中…lt :200, $gt : 100}})

五、mongodb排序

db.集合名稱.find().sort()

使用 sort() 方法對資料進行排序,sort() 方法可以通過引數指定排序的字段,並使用 1 和 -1 來指定排序的方式,其中 1 為公升序排列,而 -1 是用於降序排列。

六、mongodb索引

db.集合名稱.createindex(keys, options)

說明:語法中 key 值為你要建立的索引字段,1 為指定按公升序建立索引,如果你想按降序來建立索引指定為 -1 即可。

例項:db.col.createindex()

多欄位索引:

db.col.createindex()

MongoDB 資料庫操作

1 插入記錄 使用資料庫 如果沒有該資料庫就會建立,有就進入該模式 use use my testdb 插入資料 db.user.insert db.user.insert 顯示資料庫 show dbs my mongodb是隱式建立的資料庫 顯示表 show collections user是隱式...

mongodb的資料庫操作

mongodb 建立資料庫的語法格式如下 usedatabase name 如果資料庫不存在,則建立資料庫,否則切換到指定資料庫。1.建立資料庫 show dbs local 0.078gb test 0.078gb use exam switched to db exam show dbs loc...

MongoDB的資料庫操作 集合操作

建立資料庫 選擇和建立資料庫,如果資料庫不存在則自動建立 use articledb switched to db articledb 資料庫建立在記憶體中 show dbs admin 0.000gb config 0.000gb local 0.000gb在mongodb中,集合只有在內容插入後...