MongoDB學習筆記

數據庫的創建

use dbname

Insert

  • insertOne:插入一條數據到collection中

db.inventory.insertOne(

   { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
  • insertMany:插入多條數據到collection中

db.inventory.insertMany([

   { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
   
   { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
   
   { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
   
])
  • insert:db.inventory.insertMany 即可以插入一條數據也可以插入多條數據

Query

  • 查詢當前collection中的所有數據
db.collection.find({})
  • 查詢當前collection中name=zhangsan的數據
db.collection.find( { "name": "zhangsan" } )  // 等同於 select * from tableName where name="zhangsan"
  • 查詢當前collection中status=A或status=D的數據 【與關係型數據庫中的子查詢類似】
db.collection.find( { status: { $in : [ "A", "D" ] } })
  • 複合查詢 and
db.collection.find( { status: "A",qty: {$lt:30} } ) // 等同於 select * from tableName where status="A" and qty<30
  • 複合查詢 or
db.collection.find( { $or: [ { status: "A" },{ qty: { $lt:30 } } ] } )  //等同於 select * from tableName where status="A" or qty<30
  • 複合查詢 模糊查詢
db.lhc.find( { status: "A", $or:[ { qty: { $lt:30 } },{ item: /^p/} ] } ) // 等同於select * from tableName where status ="A" and (qty < 30 or item like '%p')
  • 查詢一個數據,參數可爲空
db.collection.findOne({}) // 等同與select * from tableName limit 0,1

db.collection.findOne( { status: "A" } ) // 等同於select * from tableName where status ="A" limit 0,1
  • 完全匹配查詢

當查詢條件是size:{ h: 14, w: 21, uom: “cm” }時,也要求各參數的順序也要一樣

sb.collection.find( { size: { h: 14, w: 21, uom: "cm" } } )
  • 嵌套查詢
db.collection.find( { "size.h": { $lt: 15} } )  //查詢size中h字段的值小於15的數據

db.collection.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" })  // 多個嵌套查詢

數組的查詢

  • 匹配一個數組

要在數組上指定相等條件,請使用查詢文檔{:},其中是要匹配的確切數組,包括元素的順序

db.collection.find( { tags: ["red", "blank"] } )  // 查詢包含red和blank字段的數組。且考慮順序

db.collection.find( { tags : { $all : [ "red","blank" ] } } ) // 查詢包含red和blank的數組,不考慮順序和其他的元素
  • 查詢數組
db.collection.find( { tags: "red" } ) // 查詢tags數組中包含字段red的數據

db.collection.find( { dim_cn: { $gt: 25 } } ) // 查詢dim_cn數組中,其值大於25的數據

db.collection.find(dim_cm:{$gt:15,$lt:20})  // 查詢dim_cm數組中,其中一個大於15 ,另一個小於20的數據

db.collection.find( { dim_cm: { $elemMatch: { $gt:22, $lt: 30 } } } )  // 查詢至少一條數據符合條件大於22 小於30

db.collectio.find( { "dim_cm.1": { $gt: 25 } } ) // 查詢dim_cm數組中第二個元素大於25的數據

db.collection.find( { "tags": { $size: 3 } } )  // 查詢tags數組中有三個元素的數據

  • 查詢數組中的文檔

數據源

db.inventory.insertMany( [
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
  • 查詢inventory數組中的符合warehouse=A且qty=5的數據,且考慮順序
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )

db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } ) // 不考慮順序
  • 查詢inventory數組中,每個對象中qty的值小於20的所有數據
db.inventory.find( { "instock.qty": { $lte: 20 } } )
  • 查詢inventory數組中,第一個對象的qty值小於20的數據
db.inventory.find( { "instock.0.qty": { $lte: 20 } } )
  • 查詢inventory數組中,同時滿足warehouse=A且qty=5的對象
db.inventory.find( {"instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
  • 查詢inventory數組中,同時滿足qty>10且qty<20的對象
db.inventory.find( {"instock": { $elemMatch: { qty:  {$gt: 10, $lte: 20 } } } } )

db.inventory.find( {"instock.qty": { $gt: 10, $lte: 20 } } )

使用條件查詢並返回指定的字段

  • 查詢inventory數組中,status=A並返回_id,item和status字段
    1:代表返回指定的字段
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
  • 查詢inventory數組中,status=A並返回item和status字段
    0:在返回的字段中,要排除的字段
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id:0 } )
  • 查詢inventory數組中,status=A並返回item和size數組中的h字段
db.inventory.find( { status: "A" }, { item: 1, status: 1, "size.h": 1 } )
  • 查詢inventory數組中,status=A並排除size數組中的h字段
db.inventory.find( { status: "A" }, { "size.h": 0 } )
  • 查詢inventory數組中,status=A並返回item、status和instock數組中的最後一個對象
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )

查詢數據的value值爲null或key爲不存在的數據

  • 數據源
db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])
  • 查詢不包含item字段的數據
db.inventory.find( { item: null } )  

結果

<!--{ "_id" : 1, "item" : null }-->
<!--{ "_id" : 2 }-->
  • 查找item字段爲null數據
<!--10在mongodb中代表null-->
db.inventory.find( { item: { $type: 10 } } )
  • 查找不包含item字段的數據
db.inventory.find(  {item: { $exists: false } } )

在mongo shell中進行迭代操作

  • 對數據進行迭代
var a = db.inventory.find({})

while(a.hasNext()) {
	printjson(a.next())
}
  • 通過下標進行迭代
var myCursor = db.inventory.find( {} );
var documentArray = myCursor.toArray();
var myDocument = documentArray[1];
printjsono(myDocument)

Update

  • 修改一條數據:修改size中h字段的值爲20,status字段的值爲Z,且添加修改時間,條件爲item=planner
update inventory set h=20,status='Z' where item='planner'
db.inventory.updateOne({
	item: "planner",
},
{
	$set: {
	"size.h": 20,
	status: "Z"
	},
	$currentDate: {lastModified:true}
})
  • 修改多條數據:當qty<100時,修改size數組中的h的值爲666,status=‘D’
update inventory set h=666,status='D' where qty<100
db.inventory.updateMany({qty:{$lte:50}},
{
	$set: {"size.h":666,status: "D"},
	$currentDate: {lastModified: true}
})
  • 替換文檔
db.inventory.replaceOne(
   { item: "paper" },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
  • 總結
    • updateOne(): 對單個文檔的修改
    • updateMany():對多個文檔地修改
    • update():對單個文檔的操作

Delete

  • 刪除多個
db.collection.deleteMany({}}  //等同於 delete * from tableName

指定條件並進行多個刪除

db.inventory.deleteMany( { "size.h": { $lte: 20 } } )  // 等同於 delete * from tableName where h<20
  • 刪除一個
db.inventory.deleteOne({item:"planner"})  // 等同於 delete * from tableName where item='planner'

批量寫入操作

當對有序的數據進行寫入操作時,若寫入過程中出現error,則停止後續的寫入操作

當對無序的數據進行寫入操作的時候,若寫入過程中出現error,mongoDB會繼續執行批量寫入操作

  • 批量寫入的方法
    • insertOne
    • updateOne
    • updateMany
    • replaceOne
    • deleteOne
    • deleteMany

SQL與MOngoDB關係對映關係

k5qmWD.png

  • 常見查詢語句的對應

k5LJBR.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章