《MongoDB高手課》學習記錄(第十一天)

第十一天

今天繼續CRUD部分的複習。javascript

查詢數據

昨天還遺留3個內容沒複習,今天補上java

查詢 null 值

生成幾條測試數據shell

db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])

若是下面這樣寫,除返回值是 null 的文檔以外,還返回不包含 item 字段的文檔。數據庫

> db.inventory.find( { item: null } )
{ "_id" : 1, "item" : null }
{ "_id" : 2 }

那麼,若是我只想返回值是 null 的數據呢?數組

> db.inventory.find( { item : { $type: 10 } } )
{ "_id" : 1, "item" : null }

$type 爲 10 表示的是MongoDB內置類型爲 null,更多的類型以下post

類型 數字 別名
Double 1 「double」
String 2 「string」
Object 3 「object」
Array 4 「array」
Binary data 5 「binData」
ObjectId 7 「objectId」
Boolean 8 「bool」
Date 9 「date」
Null 10 「null」
Regular Expression 11 「regex」
JavaScript 13 「javascript」
JavaScript (with scope) 15 「javascriptWithScope」
32-bit integer 16 「int」
Timestamp 17 「timestamp」
64-bit integer 18 「long」
Decimal128 19 「decimal」
Min key -1 「minKey」
Max key 127 「maxKey」

查詢不包含某個字段的文檔

> db.inventory.find( { item : { $exists: false } } )
{ "_id" : 2 }

控制返回的字段列表

如今數據是這樣的測試

> db.inventory.find()
{ "_id" : 1, "item" : null }
{ "_id" : 2 }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88b"), "item" : "journal", "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "instock" : [ { "warehouse" : "A", "qty" : 5 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88c"), "item" : "notebook", "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88d"), "item" : "paper", "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 60 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88e"), "item" : "planner", "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "instock" : [ { "warehouse" : "A", "qty" : 40 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88f"), "item" : "postcard", "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
>

若是我要item與status其餘不想要,怎麼辦。傳入第二個對象。code

> db.inventory.find({}, {item: 1, status: 1, _id: 0})
{ "item" : null }
{  }
{ "item" : "journal", "status" : "A" }
{ "item" : "notebook", "status" : "A" }
{ "item" : "paper", "status" : "D" }
{ "item" : "planner", "status" : "D" }
{ "item" : "postcard", "status" : "A" }

_id 字段比較特殊,必定要顯示的指定,不然默認一直顯示,而其餘這段,若是不寫,就不顯示。
反過來,若是我只排除item與status不顯示,而其餘都顯示呢?對象

> db.inventory.find( { }, { item: 0, status: 0 } )
{ "_id" : 1 }
{ "_id" : 2 }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88b"), "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "instock" : [ { "warehouse" : "A", "qty" : 5 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88c"), "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88d"), "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 60 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88e"), "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "instock" : [ { "warehouse" : "A", "qty" : 40 } ] }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88f"), "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }

可是,若是使用排除模式,就不能單獨控制 _id 的顯示了,不然報錯,也就是不能混着用,要麼排除,要麼顯示ip

> db.inventory.find( { }, { item: 0, status: 0, _id: 1 } )
Error: error: {
        "ok" : 0,
        "errmsg" : "Projection cannot have a mix of inclusion and exclusion.",
        "code" : 2,
        "codeName" : "BadValue"
}

若是是返回內嵌文檔,好比只返回size.uom

> db.inventory.find({},{"size.uom": 1, _id: 0})
{  }
{  }
{ "size" : { "uom" : "cm" } }
{ "size" : { "uom" : "in" } }
{ "size" : { "uom" : "in" } }
{ "size" : { "uom" : "cm" } }
{ "size" : { "uom" : "cm" } }

若是控制返回數組內容,好比instock數組中的qty

> db.inventory.find({},{"instock.qty": 1,_id: 0})
{  }
{  }
{ "instock" : [ { "qty" : 5 } ] }
{ "instock" : [ { "qty" : 5 } ] }
{ "instock" : [ { "qty" : 60 } ] }
{ "instock" : [ { "qty" : 40 } ] }
{ "instock" : [ { "qty" : 15 }, { "qty" : 35 } ] }

那麼,若是隻想返回最後一組呢,好比上面qty:35那組有2個,我只要最後一個

> db.inventory.find({},{instock: { $slice: -1 },"instock.qty": 1,_id: 0})
{  }
{  }
{ "instock" : [ { "qty" : 5 } ] }
{ "instock" : [ { "qty" : 5 } ] }
{ "instock" : [ { "qty" : 60 } ] }
{ "instock" : [ { "qty" : 40 } ] }
{ "instock" : [ { "qty" : 35 } ] }

字段別名

插入數據

這塊的內容,咱們前面已經用過幾回了,insert插入一個文檔,insertMany插入多個文檔。每一個新插入的文檔都會有一個_id作爲主鍵,類型是ObjectId(),由系統自動生成。

插入1個

db.inventory.insertOne({ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] })

插入多個

> db.inventory.insertMany( [
  { item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
  { item: "notebook", status: "A",  size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
  { item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
  { item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
  { item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

更新數據

更新1個文檔

若是要更新數據,也要傳入2個對象,第一個是條件,第二是要更新字段及值。

> db.inventory.updateOne(
    { item: "paper" },
    {
      $set: { "size.uom": "cm", status: "P" },
      $currentDate: { lastModified: true }
    }
 )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.inventory.findOne(    { item: "paper" } )
{
        "_id" : ObjectId("5e089a29667bbe281bb9a88d"),
        "item" : "paper",
        "status" : "P",
        "size" : {
                "h" : 8.5,
                "w" : 11,
                "uom" : "cm"
        },
        "instock" : [
                {
                        "warehouse" : "A",
                        "qty" : 60
                }
        ],
        "lastModified" : ISODate("2019-12-29T12:58:35.907Z")
}

$currentDate: { lastModified: true },是指定若是存在lastModified字段就是用currentDate,也就是當前時間賦值,若是不存在,剛建立一個。

> db.inventory.updateOne(
  { item: "planner" },
  {      
    $set: { 
      "size.uom": "cm", 
      status: "P"
    }
  } 
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.inventory.findOne(    { item: "planner" } )
{
        "_id" : ObjectId("5e089a29667bbe281bb9a88e"),
        "item" : "planner",
        "status" : "P",
        "size" : {
                "h" : 22.85,
                "w" : 30,
                "uom" : "cm"
        },
        "instock" : [
                {
                        "warehouse" : "A",
                        "qty" : 40
                }
        ]
}

更新多個文檔

> db.inventory.updateMany(    { "instock.qty": { $lt: 50 } },    {      $set: { "size.uom": "in", status: "P" },      $currentDate: { lastModified: true }    } )
{ "acknowledged" : true, "matchedCount" : 9, "modifiedCount" : 9 }
> db.inventory.find({ "instock.qty": { $lt: 50 } })
{ "_id" : ObjectId("5e089a29667bbe281bb9a88b"), "item" : "journal", "status" : "P", "size" : { "h" : 14, "w" : 21, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 5 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88c"), "item" : "notebook", "status" : "P", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "C", "qty" : 5 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88e"), "item" : "planner", "status" : "P", "size" : { "h" : 22.85, "w" : 30, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 40 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88f"), "item" : "postcard", "status" : "P", "size" : { "h" : 10, "w" : 15.25, "uom" : "in" }, "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e08a1a1667bbe281bb9a890"), "item" : "journal", "status" : "P", "size" : { "h" : 14, "w" : 21, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 5 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e08a1a1667bbe281bb9a891"), "item" : "notebook", "status" : "P", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "C", "qty" : 5 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e08a1a1667bbe281bb9a893"), "item" : "planner", "status" : "P", "size" : { "h" : 22.85, "w" : 30, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 40 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e08a1a1667bbe281bb9a894"), "item" : "postcard", "status" : "P", "size" : { "h" : 10, "w" : 15.25, "uom" : "in" }, "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
{ "_id" : ObjectId("5e08a1f1667bbe281bb9a895"), "item" : "journal", "status" : "P", "size" : { "h" : 14, "w" : 21, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 5 } ], "lastModified" : ISODate("2019-12-29T13:09:08.872Z") }
>

替換文檔

注意,_id字段沒法替換

> db.inventory.find({ item: "paper"})
{ "_id" : ObjectId("5e089a29667bbe281bb9a88d"), "item" : "paper", "status" : "P", "size" : { "h" : 8.5, "w" : 11, "uom" : "cm" }, "instock" : [ { "warehouse" : "A", "qty" : 60 } ], "lastModified" : ISODate("2019-12-29T12:58:35.907Z") }
{ "_id" : ObjectId("5e08a1a1667bbe281bb9a892"), "item" : "paper", "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 60 } ] }
> db.inventory.replaceOne(
   { item: "paper" },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
> db.inventory.find({ item: "paper"})
{ "_id" : ObjectId("5e089a29667bbe281bb9a88d"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 40 } ] }
{ "_id" : ObjectId("5e08a1a1667bbe281bb9a892"), "item" : "paper", "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "instock" : [ { "warehouse" : "A", "qty" : 60 } ] }
>

刪除數據

多個刪除

> db.inventory.deleteMany( { status: "P" } )
{ "acknowledged" : true, "deletedCount" : 9 }
> db.inventory.find( {} )
{ "_id" : 1, "item" : null }
{ "_id" : 2 }
{ "_id" : ObjectId("5e089a29667bbe281bb9a88d"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 40 } ] }
>

注意,注意,若是你不寫條件,下面這個操做將刪除全部文檔

db.inventory.deleteMany({})

單個刪除

> db.inventory.deleteOne( { status: "D" } )
{ "acknowledged" : true, "deletedCount" : 1 }
> db.inventory.find( { status: "D" } )
>

刪除表

只在mongoshell下可用

> db.inventory.drop()
true

刪除數據庫

只在mongoshell下可用,刪庫跑路,刪除當前數據庫

> show dbs
admin   0.000GB
config  0.000GB
foo     0.000GB
local   0.000GB
mock    0.047GB
> use foo
switched to db foo
> db.dropDatabase()
{ "dropped" : "foo", "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mock    0.047GB
>

總結

今天完成CRUD全部基礎操做的複習,固然還有不少高級的內容,先不糾結,先完成課程。明天繼續複習聚合查詢部分。

相關文章
相關標籤/搜索