Hướng dẫn mongodb shell - vỏ mongodb

Cài đặt Mongo trên Windows

  1. Download trên trang chủ Mongo, chọn phiên bản phù hợp tại đây.

    Ở đây mình chọn version 4.2.1 (current release). OS: Windows x64 x64.

  2. Sau khi tải về thì chạy file .msi để cài đặt.

    • Cài đặt MongoDB trên Windows như service. Nghĩa là mỗi khi khởi động PC, nó sẽ chạy ngầm như một service trên windows.

    • Sẽ có 2 tuỳ chọn. Ở đây sẽ chọn Run service as Network Service user để kết nối với MongoDB trong tất cả network.Run service as Network Service user để kết nối với MongoDB trong tất cả network.

  3. Bỏ tùy chỉnh cài đặt MongoDB Compass nêu sử dụng Robo 3T.MongoDB Compass nêu sử dụng Robo 3T.

  4. Restart PC và kiểm tra cài đặt MongoDB cài đặt thành công hay chưa trong Windows Task ManagerWindows Task Manager

    Hướng dẫn mongodb shell - vỏ mongodb

  5. Để tương tác với mongoDB qua command line. Thì phải cài đặt biến môi trường. Tìm tới folder cài đặt, mặc định thì sẽ là

    [
        {
            "color":"black",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    255,
                    1
                ],
                "hex":"#000"
            }
        },
        {
            "color":"white",
            "category":"value",
            "code":{
                "rgba":[
                    0,
                    0,
                    0,
                    1
                ],
                "hex":"#FFF"
            }
        },
        {
            "color":"red",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    0,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"blue",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    0,
                    0,
                    255,
                    1
                ],
                "hex":"#00F"
            }
        },
        {
            "color":"yellow",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"green",
            "category":"hue",
            "type":"secondary",
            "code":{
                "rgba":[
                    0,
                    255,
                    0,
                    1
                ],
                "hex":"#0F0"
            }
        }
    ]
    
    7.

    Sau đó, mở

    [
        {
            "color":"black",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    255,
                    1
                ],
                "hex":"#000"
            }
        },
        {
            "color":"white",
            "category":"value",
            "code":{
                "rgba":[
                    0,
                    0,
                    0,
                    1
                ],
                "hex":"#FFF"
            }
        },
        {
            "color":"red",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    0,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"blue",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    0,
                    0,
                    255,
                    1
                ],
                "hex":"#00F"
            }
        },
        {
            "color":"yellow",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"green",
            "category":"hue",
            "type":"secondary",
            "code":{
                "rgba":[
                    0,
                    255,
                    0,
                    1
                ],
                "hex":"#0F0"
            }
        }
    ]
    
    8 hoặc
    [
        {
            "color":"black",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    255,
                    1
                ],
                "hex":"#000"
            }
        },
        {
            "color":"white",
            "category":"value",
            "code":{
                "rgba":[
                    0,
                    0,
                    0,
                    1
                ],
                "hex":"#FFF"
            }
        },
        {
            "color":"red",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    0,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"blue",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    0,
                    0,
                    255,
                    1
                ],
                "hex":"#00F"
            }
        },
        {
            "color":"yellow",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"green",
            "category":"hue",
            "type":"secondary",
            "code":{
                "rgba":[
                    0,
                    255,
                    0,
                    1
                ],
                "hex":"#0F0"
            }
        }
    ]
    
    9 lên gõ lệnh
    $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
    
    0 để kiểm tra

  6. Một số câu lệnh cơ bản

    • $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      1: dùng để hiển thị danh sách các database đang tồn tại. Mặc định ban đầu sẽ có 3 database của hệ thống là: admin, config và local.

    • $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      2: hiển thị các collection đang tồn tại

    • $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      3: hiển thị danh sách database đang sử dụng. Vì bạn chưa có database nào nên mặc định database mặc định cho bạn sẽ là test.

    • $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      4: chuyển sang sử dụng database với tên là
      $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      5 (tên này tùy bạn chọn).

    • $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      6: dùng để xóa database hiện tại

    • $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      7: dùng để tạo mới một Collection trong MongoDB (cái này tương tự như Table trong MySQL).

    Cách select, insert, update collections .Mọi thứ bắt đầu từ

    $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
    
    8

    • $ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json
      
      9: thêm mới một Document vào MongoDB (cái này tương như như Row trong MySQL)

    • 2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
      2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
      
      0: thêm mới hoặc cập nhật một document trong collection

Truy vấn cơ bản trong MongoDB

1. Mongo Shell

2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
1 là một JavaScript interface tương tác với MongoDB. Bạn có thể sử dụng mongo shell để
2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
2 và
2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
3 cũng như thực hiện các
2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
4.

Thực một số lệnh truy vấn cơ bản trong mongo shell. Đầu tiên, tôi sẽ sử dụng lệnh

2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
5 để xem tất cả documents trong mongo shell.

admin@TuanNM MINGW64 /d/Drive/Documens
$ mongoimport --help
Usage:
  mongoimport  

Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.

See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information.

Tôi sẽ thực hiện một số thao tác

2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
6 trong trình Mongo Shell. Để làm như vậy, tôi có thể sử dụng các lệnh import trong Mongo. Các lệnh import Mongo có thể thực hiện công việc import dữ liệu nằm trong file
2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
7, v.v.

2. Insert

Cách 1: Tạo một collections có tên

2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
8 và insert 26 000 row vào đó.

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })

Import data từ file

2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
9. Tạo một file json với tên
$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
0 có cấu trúc như sau:

[
    {
        "color":"black",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                255,
                1
            ],
            "hex":"#000"
        }
    },
    {
        "color":"white",
        "category":"value",
        "code":{
            "rgba":[
                0,
                0,
                0,
                1
            ],
            "hex":"#FFF"
        }
    },
    {
        "color":"red",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                0,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"blue",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                0,
                0,
                255,
                1
            ],
            "hex":"#00F"
        }
    },
    {
        "color":"yellow",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"green",
        "category":"hue",
        "type":"secondary",
        "code":{
            "rgba":[
                0,
                255,
                0,
                1
            ],
            "hex":"#0F0"
        }
    }
]

Sau đó thực hiện

$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
1 nó vào mongo. Open terminal tại nơi chứa file
$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
0

$ mongoimport --db mongo_color --collection colors --jsonArray --file colors.json

Result:

2019-11-26T11:12:38.063+0700    connected to: mongodb://localhost/
2019-11-26T11:12:38.082+0700    6 document(s) imported successfully. 0 document(s) failed to import.
  • $ mongo
    > use mongo_color
    switched to db mongo_color
    > db.colors.insert({
        "color": "custome",
        "category": "hue",
        "type": "primary",
        "code": {
            "rgba": [255, 1, 255, 1],
            "hex": "#FF1"
        }
    })
    
    3: tên database
  • $ mongo
    > use mongo_color
    switched to db mongo_color
    > db.colors.insert({
        "color": "custome",
        "category": "hue",
        "type": "primary",
        "code": {
            "rgba": [255, 1, 255, 1],
            "hex": "#FF1"
        }
    })
    
    4: tên collection
  • $ mongo
    > use mongo_color
    switched to db mongo_color
    > db.colors.insert({
        "color": "custome",
        "category": "hue",
        "type": "primary",
        "code": {
            "rgba": [255, 1, 255, 1],
            "hex": "#FF1"
        }
    })
    
    5: nguồn đầu vào là một mảng JSON
  • $ mongo
    > use mongo_color
    switched to db mongo_color
    > db.colors.insert({
        "color": "custome",
        "category": "hue",
        "type": "primary",
        "code": {
            "rgba": [255, 1, 255, 1],
            "hex": "#FF1"
        }
    })
    
    6: tên file data

Hoặc cách insert từ mongo shell

$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})

3. Update

Update một row trong mongoDB thì như thế nào?

Tôi sẽ thêm một property mới

$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
7 với value là
$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
8 vào color
$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
9 đã insert ở trên.

> db.colors.update({
    "color": "custome"
}
, {
    $set: {
        "manuallyCreated": "True"
    }
})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Truy vấn các color có

$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
7 là
$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
8

> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }

Update color name

$ mongo
> use mongo_color
switched to db mongo_color
> db.colors.insert({
    "color": "custome",
    "category": "hue",
    "type": "primary",
    "code": {
        "rgba": [255, 1, 255, 1],
        "hex": "#FF1"
    }
})
9 thành
> db.colors.update({
    "color": "custome"
}
, {
    $set: {
        "manuallyCreated": "True"
    }
})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
3, điều kiện là một là
> db.colors.update({
    "color": "custome"
}
, {
    $set: {
        "manuallyCreated": "True"
    }
})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
4:

> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }

Trường hợp không set một value cho property cụ thể, thì nó bị ghi đè:

db.colors.update({"color":"red"},{"color":"test"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"color":"test"})
{ "_id" : ObjectId("5ddca636c0ca555ba6529b59"), "color" : "test" }

Một số ví dụ về update user collections:

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
0
  • > db.colors.update({
        "color": "custome"
    }
    , {
        $set: {
            "manuallyCreated": "True"
        }
    })
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    
    5: nếu có dữ liệu phù hợp điều kiện, thì sẽ update tất cả thay vì một
  • > db.colors.update({
        "color": "custome"
    }
    , {
        $set: {
            "manuallyCreated": "True"
        }
    })
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    
    6: greater than
  • > db.colors.update({
        "color": "custome"
    }
    , {
        $set: {
            "manuallyCreated": "True"
        }
    })
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    
    7: less than
  • > db.colors.update({
        "color": "custome"
    }
    , {
        $set: {
            "manuallyCreated": "True"
        }
    })
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    
    8: ví dụ age = age + 3 để tăng giá trị của một field được chỉ định.

4. Truy vấn > db.colors.update({ "color": "custome" } , { $set: { "manuallyCreated": "True" } }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 9 trong MongoDB

1. Find() & Pretty format trong mongo shell:

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
1

Example:

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
2

Pretty formating:

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
3

2. Toán tử

Xem docs - https://docs.mongodb.com/manual/reference/operator/query/

OperationSyntaxExampleSQL
Equality
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
0
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
1
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
2
Less Than
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
3
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
4
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
5
Less Than Equals
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
6
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
7
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
8
Greater Than
> db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
9
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
0
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
1
Greater Than Equals
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
2
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
3
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
4
Not Equals
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
5
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
6
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
7
In
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
8
> db.colors.update({
    "_id": ObjectId("5ddca6b6993d1592c2ace363")
}, {
    $set: {
        "color": "blackpink"
    }
})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"manuallyCreated":"True"})

{ "_id" : ObjectId("5ddca6b6993d1592c2ace363"), "color" : "blackpink", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
9
db.colors.update({"color":"red"},{"color":"test"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.colors.find({"color":"test"})
{ "_id" : ObjectId("5ddca636c0ca555ba6529b59"), "color" : "test" }
0

3. Columns

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
4

Example:

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
5

4. AND

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
6

Example:

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
7

4. OR

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
8

Example:

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mongotest   0.001GB
mylearning  0.000GB

> use mongo test
switched to db mongotest

> for (i = 0; i <= 26000; i++) {
    db.Numbers.insert({
        "number": i
    })
}
WriteResult({ "nInserted" : 1 })
9

5. Limit & Offset

Limit

[
    {
        "color":"black",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                255,
                1
            ],
            "hex":"#000"
        }
    },
    {
        "color":"white",
        "category":"value",
        "code":{
            "rgba":[
                0,
                0,
                0,
                1
            ],
            "hex":"#FFF"
        }
    },
    {
        "color":"red",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                0,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"blue",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                0,
                0,
                255,
                1
            ],
            "hex":"#00F"
        }
    },
    {
        "color":"yellow",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"green",
        "category":"hue",
        "type":"secondary",
        "code":{
            "rgba":[
                0,
                255,
                0,
                1
            ],
            "hex":"#0F0"
        }
    }
]
0

Limit và offset

[
    {
        "color":"black",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                255,
                1
            ],
            "hex":"#000"
        }
    },
    {
        "color":"white",
        "category":"value",
        "code":{
            "rgba":[
                0,
                0,
                0,
                1
            ],
            "hex":"#FFF"
        }
    },
    {
        "color":"red",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                0,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"blue",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                0,
                0,
                255,
                1
            ],
            "hex":"#00F"
        }
    },
    {
        "color":"yellow",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"green",
        "category":"hue",
        "type":"secondary",
        "code":{
            "rgba":[
                0,
                255,
                0,
                1
            ],
            "hex":"#0F0"
        }
    }
]
1

6. Sort

Ascending Sort

[
    {
        "color":"black",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                255,
                1
            ],
            "hex":"#000"
        }
    },
    {
        "color":"white",
        "category":"value",
        "code":{
            "rgba":[
                0,
                0,
                0,
                1
            ],
            "hex":"#FFF"
        }
    },
    {
        "color":"red",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                0,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"blue",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                0,
                0,
                255,
                1
            ],
            "hex":"#00F"
        }
    },
    {
        "color":"yellow",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"green",
        "category":"hue",
        "type":"secondary",
        "code":{
            "rgba":[
                0,
                255,
                0,
                1
            ],
            "hex":"#0F0"
        }
    }
]
2

Descending Sort

[
    {
        "color":"black",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                255,
                1
            ],
            "hex":"#000"
        }
    },
    {
        "color":"white",
        "category":"value",
        "code":{
            "rgba":[
                0,
                0,
                0,
                1
            ],
            "hex":"#FFF"
        }
    },
    {
        "color":"red",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                0,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"blue",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                0,
                0,
                255,
                1
            ],
            "hex":"#00F"
        }
    },
    {
        "color":"yellow",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"green",
        "category":"hue",
        "type":"secondary",
        "code":{
            "rgba":[
                0,
                255,
                0,
                1
            ],
            "hex":"#0F0"
        }
    }
]
3

Làm thế nào để backup và restore database trong MongoDB.

Backup

Tạo một folder để chứa file backup.

  • Command backup tất cả database:

    [
        {
            "color":"black",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    255,
                    1
                ],
                "hex":"#000"
            }
        },
        {
            "color":"white",
            "category":"value",
            "code":{
                "rgba":[
                    0,
                    0,
                    0,
                    1
                ],
                "hex":"#FFF"
            }
        },
        {
            "color":"red",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    0,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"blue",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    0,
                    0,
                    255,
                    1
                ],
                "hex":"#00F"
            }
        },
        {
            "color":"yellow",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"green",
            "category":"hue",
            "type":"secondary",
            "code":{
                "rgba":[
                    0,
                    255,
                    0,
                    1
                ],
                "hex":"#0F0"
            }
        }
    ]
    
    4
  • Command backup với một database cụ thể:

    [
        {
            "color":"black",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    255,
                    1
                ],
                "hex":"#000"
            }
        },
        {
            "color":"white",
            "category":"value",
            "code":{
                "rgba":[
                    0,
                    0,
                    0,
                    1
                ],
                "hex":"#FFF"
            }
        },
        {
            "color":"red",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    0,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"blue",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    0,
                    0,
                    255,
                    1
                ],
                "hex":"#00F"
            }
        },
        {
            "color":"yellow",
            "category":"hue",
            "type":"primary",
            "code":{
                "rgba":[
                    255,
                    255,
                    0,
                    1
                ],
                "hex":"#FF0"
            }
        },
        {
            "color":"green",
            "category":"hue",
            "type":"secondary",
            "code":{
                "rgba":[
                    0,
                    255,
                    0,
                    1
                ],
                "hex":"#0F0"
            }
        }
    ]
    
    5

Restore

Lệnh restore một database đã được backup:

[
    {
        "color":"black",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                255,
                1
            ],
            "hex":"#000"
        }
    },
    {
        "color":"white",
        "category":"value",
        "code":{
            "rgba":[
                0,
                0,
                0,
                1
            ],
            "hex":"#FFF"
        }
    },
    {
        "color":"red",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                0,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"blue",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                0,
                0,
                255,
                1
            ],
            "hex":"#00F"
        }
    },
    {
        "color":"yellow",
        "category":"hue",
        "type":"primary",
        "code":{
            "rgba":[
                255,
                255,
                0,
                1
            ],
            "hex":"#FF0"
        }
    },
    {
        "color":"green",
        "category":"hue",
        "type":"secondary",
        "code":{
            "rgba":[
                0,
                255,
                0,
                1
            ],
            "hex":"#0F0"
        }
    }
]
6

End