Truy vấn trong MongoDB Compass

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.

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

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

  • Để 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

  • 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.