《MongoDB高手課》學習記錄(次日)

踩坑記錄

操做系統 windows 10 家庭中文版 1909 正則表達式

  1. 安裝 windows 版本的時候,有一個安裝 MongoDB Compass 的選項,若是你選了,恭喜你,125M包,就本地這下載速度,能下個幾個小時。
  2. windows 版本安裝過程當中會自動新建服務,但,但,自動創建的 MongoDB 服務的 登陸身份 確實此帳戶,結果服務說啥也起不來。果斷忽略,而後將登陸身份改成 本地系統帳戶 ,完美解決。

image.png
固然,若是是服務器,仍是得搞成 Network Service
image.pngmongodb

次日

計劃

今天要學習《06 MongoDB基本操做》shell

Mongo的鏈接地址格式數據庫

mongodb://username:password@host[:port]/database?<options>

基本操做

如下都在shell環境下windows

查看當前數據庫

> db
mock

切換數據庫

> use test

查看當前表

> show collections
> show tables

插入一條記錄

> db.fruit.insertOne({name: "apple"})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5dfcae3eebbe93035d7c6b55")
}

插入多條記錄

> db.fruit.insertMany([
... {name: "apple"},
... {name: "pear"},
... {name: "orage"}
... ])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5dfcb07debbe93035d7c6b56"),
                ObjectId("5dfcb07debbe93035d7c6b57"),
                ObjectId("5dfcb07debbe93035d7c6b58")
        ]
}

檢索記錄

至關於 select 返回的是遊標數組

> db.fruit.find()
{ "_id" : ObjectId("5dfcae3eebbe93035d7c6b55"), "name" : "apple" }
{ "_id" : ObjectId("5dfcb07debbe93035d7c6b56"), "name" : "apple" }
{ "_id" : ObjectId("5dfcb07debbe93035d7c6b57"), "name" : "pear" }
{ "_id" : ObjectId("5dfcb07debbe93035d7c6b58"), "name" : "orage" }

單條件查詢(一個條件)

> db.orders.find({"name":"Destinee Schneider"})
{
    "_id": ObjectId("5dbe7a545368f69de2b4d36e"),
    "street": "493 Hilll Curve",
    "city": "Champlinberg",
    "state": "Texas",
    "country": "Malaysia",
    "zip": "24344-1715",
    "phone": "425.956.7743 x4621",
    "name": "Destinee Schneider",
    "userId": 3573,
    "orderDate": ISODate("2019-03-26T03:20:08.805Z"),
    "status": "created",
    "shippingFee": NumberDecimal("8.00"),
    "orderLines": [{
        "product": "Refined Fresh Tuna",
        "sku": "2057",
        "qty": 25,
        "price": NumberDecimal("56.00"),
        "cost": NumberDecimal("46.48")
    }, {
        "product": "Refined Concrete Ball",
        "sku": "1738",
        "qty": 61,
        "price": NumberDecimal("47.00"),
        "cost": NumberDecimal("47")
    }, {
        "product": "Rustic Granite Towels",
        "sku": "500",
        "qty": 62,
        "price": NumberDecimal("74.00"),
        "cost": NumberDecimal("62.16")
    }, {
        "product": "Refined Rubber Salad",
        "sku": "1400",
        "qty": 73,
        "price": NumberDecimal("93.00"),
        "cost": NumberDecimal("87.42")
    }, {
        "product": "Intelligent Wooden Towels",
        "sku": "5674",
        "qty": 72,
        "price": NumberDecimal("84.00"),
        "cost": NumberDecimal("68.88")
    }, {
        "product": "Refined Steel Bacon",
        "sku": "5009",
        "qty": 8,
        "price": NumberDecimal("53.00"),
        "cost": NumberDecimal("50.35")
    }],
    "total": NumberDecimal("407")
}

多條件 and 查詢(1)

> db.orders.find({"country":"Italy","city":"North Elliot"})
{
    "_id": ObjectId("5dbe7a5b50fc769de3e1a0f8"),
    "street": "5769 Von Trail",
    "city": "North Elliot",
    "state": "South Dakota",
    "country": "Italy",
    "zip": "93708-8445",
    "phone": "1-390-726-3544",
    "name": "Alexys Swift",
    "userId": 9628,
    "orderDate": ISODate("2019-07-25T07:21:14.661Z"),
    "status": "completed",
    "shippingFee": NumberDecimal("10.00"),
    "orderLines": [{
        "product": "Refined Plastic Computer",
        "sku": "5065",
        "qty": 50,
        "price": NumberDecimal("82.00"),
        "cost": NumberDecimal("71.34")
    }, {
        "product": "Generic Granite Gloves",
        "sku": "4352",
        "qty": 10,
        "price": NumberDecimal("37.00"),
        "cost": NumberDecimal("32.56")
    }, {
        "product": "Practical Rubber Chicken",
        "sku": "2035",
        "qty": 29,
        "price": NumberDecimal("84.00"),
        "cost": NumberDecimal("68.04")
    }, {
        "product": "Handcrafted Fresh Bike",
        "sku": "2276",
        "qty": 51,
        "price": NumberDecimal("77.00"),
        "cost": NumberDecimal("64.68")
    }, {
        "product": "Sleek Cotton Car",
        "sku": "5257",
        "qty": 6,
        "price": NumberDecimal("94.00"),
        "cost": NumberDecimal("79.9")
    }, {
        "product": "Practical Fresh Mouse",
        "sku": "9593",
        "qty": 67,
        "price": NumberDecimal("98.00"),
        "cost": NumberDecimal("96.04")
    }, {
        "product": "Incredible Plastic Ball",
        "sku": "7804",
        "qty": 22,
        "price": NumberDecimal("45.00"),
        "cost": NumberDecimal("39.15")
    }, {
        "product": "Refined Plastic Bike",
        "sku": "9804",
        "qty": 27,
        "price": NumberDecimal("22.00"),
        "cost": NumberDecimal("18.92")
    }, {
        "product": "Handcrafted Concrete Shoes",
        "sku": "1473",
        "qty": 1,
        "price": NumberDecimal("49.00"),
        "cost": NumberDecimal("44.1")
    }],
    "total": NumberDecimal("588")
}

多條件 and 查詢(2)

> db.orders.find({$and: [{"country":"Italy"},{"city":"North Elliot"}]})

多條件 or 查詢

> db.orders.find({$or:[{"country":"Italy"},{"city":"North Elliot"}]})

正則表達式查詢

> db.orders.find({"country": /^I/})

查詢條件

SQL MQL
a=1 {a: 1}
a<>1 {a: {$ne: 1}}
a>1 {a: {$gt: 1}}
a<1 {a: {$lt: 1}}
a>=1 {a: {$gte: 1}}
a<=1 {a: {$lte: 1}}
a=1 and b=1 {a:1,b:1} 或 {$and: [{a:1},{b:1}]}
a=1 or b=1 {$or:[{a:1},{b:1}]}
a=null {a: {$exists:false}}
a in (1,2,3) {a: {$in:[1,2,3]}}
a not in(1,2,3) {a: {$nin:[1,2,3]}}

搜索子文檔

> db.fruit.insertOne({
    name: "apple",
    from:{
        country: "China",
        province: "Guangdon"
    }
})

要查詢China服務器

> db.fruit.find({"from.country":"China"})
{ 
    "_id" : ObjectId("5dfcc154d432688fd475d7a4"), 
    "name" : "apple", 
    "from" : {
        "country" : "China", 
        "province" : "Guangdon"
    }
}

查詢數組中的值

> db.fruit.insert([
    {
        "name":"Apple",
        "color":["red","green"]
    },
    {
        "name":"Mango",
        "color":["yellow","green"]
    }
])

方式1

> db.fruit.find({"color":"red"})
{ "_id" : ObjectId("5dfcc543f598f9f1c332185f"), "name" : "Apple", "color" : [ "red", "green" ] }

方式2

> db.fruit.find({$or: [{"color":"red"},{"color":"yellow"}]})
{ 
    "_id" : ObjectId("5dfcc2eed432688fd475d7a6"), 
    "name" : "Mango", 
    "color" : [
        "yellow", 
        "green"
    ]
},
{ 
    "_id" : ObjectId("5dfcc543f598f9f1c332185f"), 
    "name" : "Apple", 
    "color" : [
        "red", 
        "green"
    ]
}

查詢數組中的對象

> db.movies.insertOne({
    "title": "Raiders of the Lost Ark",
    "filming_locations": [
        {
            "city": "Los Angeles",
            "state":"CA",
            "country": "USA"
        },
        {
            "city": "Rome",
            "state": "Lazio",
            "country": "Italy"
        },
        {
            "city": "Florence",
            "state": "SC",
            "country": "USA"
        }
    ]
})

一個條件

> db.movies.find({
    "filming_locations.city": "Rome"
})
{ 
    "_id" : ObjectId("5dfcc850d432688fd475d7a7"), 
    "title" : "Raiders of the Lost Ark", 
    "filming_locations" : [
        {
            "city" : "Los Angeles", 
            "state" : "CA", 
            "country" : "USA"
        }, 
        {
            "city" : "Rome", 
            "state" : "Lazio", 
            "country" : "Italy"
        }, 
        {
            "city" : "Florence", 
            "state" : "SC", 
            "country" : "USA"
        }
    ]
}

二個條件

這樣是啥也查不到的app

> db.movies.find({
    "filming_locations.city": "Rome",
    "filming_locaionts.country": "Italy"
})

應該這麼寫ide

> db.movies.find({
    "filming_locations": {
        $elemMatch: {
            "city":"Rome",
            "country": "Italy"
        }
    }
})

{ 
    "_id" : ObjectId("5dfcc850d432688fd475d7a7"), 
    "title" : "Raiders of the Lost Ark", 
    "filming_locations" : [
        {
            "city" : "Los Angeles", 
            "state" : "CA", 
            "country" : "USA"
        }, 
        {
            "city" : "Rome", 
            "state" : "Lazio", 
            "country" : "Italy"
        }, 
        {
            "city" : "Florence", 
            "state" : "SC", 
            "country" : "USA"
        }
    ]
}

控制 find 返回的字段

  • 返回全部記錄,但只要 title 字段,不要 _id 字段
> db.movies.find({},{"_id":0,"title":1})
{ 
    "title" : "Raiders of the Lost Ark"
}

對 find 返回的結果進行格式化

> db.movies.find().pretty()

要否則在SHELL下面是這樣的學習

{ "_id" : ObjectId("5dfcc850d432688fd475d7a7"), "title" : "Raiders of the Lost Ark", "filming_locations" : [ { "city" : "Los Angeles", "state" : "CA", "country" : "USA" }, { "city" : "Rome", "state" : "Lazio", "country" : "Italy" }, { "city" : "Florence", "state" : "SC", "country" : "USA" } ] }

加完變這樣了

{
    "_id" : ObjectId("5dfcc850d432688fd475d7a7"),
    "title" : "Raiders of the Lost Ark",
    "filming_locations" : [
        {
            "city" : "Los Angeles",
            "state" : "CA",
            "country" : "USA"
        },
        {
            "city" : "Rome",
            "state" : "Lazio",
            "country" : "Italy"
        },
        {
            "city" : "Florence",
            "state" : "SC",
            "country" : "USA"
        }
    ]
}

刪除文檔

刪除指定 _id 的記錄

> db.fruit.remove({"_id" : ObjectId("5dfcc543f598f9f1c3321860")})

刪除指定條件的文檔

> db.fruit.remove({"color":"red"})
WriteResult({ "nRemoved" : 2 })

刪除全部

> db.movies.remove({})

不給參數會報錯

> db.movies.remove()

更新文檔

只做用於集合中存在的文檔

更新值

將下面的 name 是 apple 的改爲 Apple

{ 
    "_id" : ObjectId("5dfcc154d432688fd475d7a4"), 
    "name" : "apple", 
    "from" : {
        "country" : "China", 
        "province" : "Guangdon"
    }
}
{ 
    "_id" : ObjectId("5dfcc2eed432688fd475d7a5"), 
    "name" : "Apple", 
    "color" : [
        "read", 
        "green"
    ]
}
{ 
    "_id" : ObjectId("5dfcc2eed432688fd475d7a6"), 
    "name" : "Mango", 
    "color" : [
        "yellow", 
        "green"
    ]
}
> db.fruit.update({"name": "apple"}, {$set: {"name": "Apple"}})

加個字段

> db.fruit.updateOne({"name":"Mango"},{$set: {"from":"China"}})

更新條件

updateOne 與 updateMany

條件 做用
$push 添加一個對象到數組底部
$pushAll 添加多個對象到數組底部
$pop 從數組底部刪除一個對象
$pull 若是知足指定條件,就從數組中刪除對象
$pullAll 若是知足任意條件,就從數組中刪除對象
$addToSet 若是不存在,就增長值到數組

刪除集合 drop

刪除了沒辦法恢復哈

> db.fruit.drop()

刪除數據庫 dropDatabase

> db.dropDatabase()

最後

今天內容有點多,就是過了一遍,十幾分鐘的課程,學了有2個多小時,接下來練習練習……

相關文章
相關標籤/搜索