mongodb 基本操作


所有操作都在 MongoDB 可視化工具 Robo 3T 軟件下演示操作

 

一:使用 insert 插入文檔:

 

插入一條數據與多條數據的格式:

1 db.<集合>.insertOne(<JSON對象>)
2 db.<集合>.insertMany([<JSON 1>, <JSON 2>, ...<JSON n>])

 

示例:

1 db.fruit.insertOne({name: "apple"})
2 
3 db.fruit.insertMany([
4         {name: "apple"},
5         {name: "pear"},
6         {name: "orange"}
7     ])

 

Robo 3T:

單條:

 

 

多條:

 

 

二:使用 find 查詢文檔:

 

查詢條件對照表:

 

 

運算符:

1 $lt  :存在並小於; 
2 $lte :存在並小於等於;
3 $gt  :存在並大於;
4 $gte :存在並大於等於;
5 $ne  :不存在或存在但不等於;
6 $in  :存在並在指定數組中;
7 $nin :不存在或不在指定數組中;
8 $or  :匹配兩個或多個條件中的一個;
9 $and :匹配全部條件;

 

查詢示例:

1 db.movies.find("year": 1975})  //單條件查詢;
2 db.movies.find({"year": 1989, "title": "Batman"})  //多條件and查詢;
3 db.movies.find({$and: [{"title": "Batman"}, {"category": "action}]})  //and查詢的另一種形式;
4 db.movies.find({$or: [{"year": 1989}, {"title": "Batman"}]})  //多條件 or 查詢;
5 db.movies.find({"title": /^B/})  //按正則表達式查找;

 

使用 find 查詢子文檔:

 1 # 插入帶有子文檔的文檔數據;
 2 db.fruit.insertOne({
 3     name: "apple",
 4     from: {
 5         country: "China",
 6         province: "Guangdon"
 7     }
 8 })
 9 
10 # 查詢:
11 db.fruit.find({"from.country": "China"})

 

Robo 3T:(上方為插入文檔的命令,下方顯示的數據需使用 db.getCollection('fruit').find({}) 查詢才會顯示結果)

 

查詢數組中的元素(例1):

 1 # 插入帶數組的文檔數據
 2 
 3 db.fruit.insert([
 4     {"name": "Apple", color: ["red", "green"]},
 5     {"name": "Mango", color: ["yellow", "green"]}
 6 ])
 7 
 8 # 查詢
 9 
10 db.fruit.find({color: "red"})  //顯示一個結果;
11 db.fruit.find({$or: [{color: "red"}, {color: "yellow"}]})  //顯示兩條結果;

 

Robo 3T:(插入的數組文檔的顯示格式)

 

查詢數組中的對象(例2):

 1 # 文檔數組中的子文檔
 2 
 3 db.movies.insertOne({
 4     "title": "Raiders of the Lost Ark",
 5     "filming_locations": [
 6         {"city": "Los Angeles", "state": "CA", "country": "USA"},
 7         {"city": "Rome", "state": "Lazio", "country": "Italy"},
 8         {"city": "Florence", "state": "SC", "country": "USA"}
 9     ]
10 })   
11 
12 
13 # 查詢城市為 Rome 的記錄
14 
15 db.movies.find({"filming_locations.city": "Rome"})

 

Robo 3T的文檔顯示:

 

查詢在同一子對象中必須同時滿足兩個條件的文檔,如下所示:

1 # 第一種方法:
2 
3 db.getCollection('movices').find({
4     "filming_locations.city": "Rome",
5     "filming_locations.country": "USA"
6 })

第二種,使用 $elemMatch 查詢,它表示必須是同一子對象滿足多個條件。

(子對象  city=Rome  ,它的  country=Italy 而不是等於 USA ,所以查詢返回的文檔為空)

 

使用 find 返回指定字段

_id 字段必須明確指定不返回(0),否則默認返回(1);

1 # 不返回 _id,只返回 title;
2 
3 db.movies.find({}, {"_id": 0, title: 1})
4 
5 # {} 表示操作所有文檔,可加條件,比如只找 category=action 的文檔數據;
6 db.movies.find({"category": "action"}, {"_id": 0, title: 1})

 

三:使用 remove 刪除文檔:

示例:

1 db.testcol.remove({a: 1})  //刪除 a 等於 1 的文檔;
2 db.testcol.remove({a: {$lt: 5}})  //刪除 a 小於 5 的文檔;
3 db.testcol.remove({})  //刪除所有文檔;
4 db.tesfcol.remove()  //錯誤寫法,報錯;

 

四:使用 update 更新文檔

 

格式: db.<集合>.update(<查詢條件>, <更新字段>) 

 

updateOne:表示無論條件匹配多少條記錄,始終只更新第一條;

updateMany:表示條件匹配多少條就更新多少條;

兩個方法在要求更新的部分中,必須加入 以下操作符中的一個,才能進行更新操作。否則會報錯:

1 $set     :更新的字段不存在時新增字段,存在則修改它;
2 $unset   :刪除字段;
3 $push    :增加一個對象到數組底部;
4 $pushAll :增加多個對象到數組底部;
5 $pop     :從數組底部刪除一個對象;
6 $pull    :如果匹配指定的值,從數組中刪除相應的對象;
7 $pullAll :如果匹配任意的值,從數據中刪除相應的對象;
8 $addToSet:如果不存在則增加一個值到數組;

例子:

1 # 插入多條數據;
2 db.fruit.insertMany([
3     {name: "apple"},
4     {name: "pear"},
5     {name: "orange"}
6 ])
7 
8 # 根據查詢條件更新文檔數據;
9 db.fruit.updateOne({name: "apple"}, {$set: {from: "China"}})

 

Robo 3T:(通過找出  name=apple  的數據后,使用  $set  進行更新)

 

 

五:使用 drop 刪除集合

 

格式: db.<集合>.drop() 

該命令將會刪除全部文檔,以及刪除集合相關的索引。

慎用 ! 刪除整個庫: db.dropDatabase() ,在當前庫下運行將刪除整個庫。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM