MONGODB INSTALLATION AND CONFIGURATION

MONGODB INSTALLATION AND CONFIGURATION


Yêu cầu

Xem thông tin của Windows:
wmic os get caption
wmic os get osarchitecture

Download MongoDB về: 
mongodb.com
=> Community
=> Windows Server 2008 R2 64bit ...
=> Update Windows while installing

How to install MongoDB?
=> Run
=> Next
=> Accept
=> Custom
=> Chọn đường dẫn để cài đặt MongoDB
=>




How to run MongoDB:
- Run MongoDB:
Crate data folder under MongoDB folder
Shift chuột phải => Open command Window here
=>

mongod.exe --dbpath path
or
mongo --dbpath path

EX: mongod --dbpath "C:\Program Files\MongoDB\Server\4.0\data"
If tên thư mục không viết dính liền (có khoảng trắng) thì như thế nào?
=> Cách xử lý: thêm dấu nháy đôi

Then, type
mongo




Download 2 phần mềm quan trọng trong quá trình học:
Console
Robomogo (Robo 3T now)

→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→
Insert Documents
db.collection.insertMany();

db.collection.insertOne();

Example:
db.user.insertMany([
    {
        username: "DungVu",
        password: "dungvu",
        age: 18,
        status: "A",
        hometown: "Hau Giang",
        height: 173
    },
    {
        username: "MaiHuynh",
        password: "maihuynh",
        age: 17,
        status: "A",
        hometown: "Can Tho"
    },
    {
        username: "TriLe",
        password: "trile",
        age: 18,
        status: "A",
        hometown: "Dong Thap",
        weight: 52
    }
]);



→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→
Manage Database and Collection in MongoDB
show dbs: show databases
use <db name>: use which database
db.createCollection("collectionName"): create a collection
show collections: show collections
db.dropDatabase(): delete a database


→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→
Tạo kết nối với Robo 3T
Create connection
=> Test
=> Save

Edit
Copy
Remove

→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→





→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→





→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→
16/11/2018
Update Documents and Remove Fields trong MongoDB
Update Documents


db.users.update{
    {username: "KhanhPham"},
    {$set: {age: 20}}
    {
        upset: true,
        multi: false
    }
}

upsert:

  • True: thêm mới record nếu tìm không thấy
  • False: không thêm mới record nếu tìm không thấy

multi:

  • True: update data on all document
  • False: update data on one document



$exists: kiểm tra có tồn tại hay không

db.users.update{
    {password: {$exists: false}},
    {$set: {password: "12345678"}},
    {
        upsert: true,
        multi: false
    }
}


Ex: Update tuổi của user có tên là "KhanhPham"

Tips:

  • Nhớ export file để backup và import lại database



Xóa đi các field không cần thiết trong document
db.users.update{
    {},
    {unset: {status: "", skills: ""}
    {
        upsert: false,
        multi: true
    }
}

Muốn loại bỏ cột trong document
db.users.update{
    {},
    {unset: {age: ""}},
    {
        upsert: false,
        multi: true
    }
}



Delete Document trong MongoDB
Xóa bỏ document
Search: MongoDB remove document => Chọn đúng version => docs
db.collection.remove()
db.collection.deleteOne()
db.collection.deleteMany()

justOne:

  • True: Xóa 1 document
  • False: Xóa nhiều document



Toán tử so sánh:
$eq: bằng - equal
$ne: không bằng - not equal
$gt: lớn hơn - greater than
$gte: lơn hơn hoặc bằng - greater than or equal
$lt: nhỏ hơn - less than
$lte: nhỏ hơn hoặc bằng - less than or equal
$in: trong - in
$nin: ngoài - not in



Ex: Xóa 1 document có tên user là "Nice"
db.users.remove{
    {user_name: "Nice"},
    {
        justOne: true
    }
}

Xóa những người có tuổi là 20
db.users.remove{
    {age: {$eq: 20}},
    {
        justOne: true
    }
}

Xóa những người có tuổi lớn hơn hoặc bằng 40
db.users.remove{
    {age: {$gte: 40}},
    {
        justOne: true
    }
}

Xóa những người có tuổi nhỏ hơn 30
db.users.remove{
    {age: {$lt: 30}},
    {
        justOne: false
    }
}

Xóa những người có tuổi lớn hơn 30 và nhỏ hơn 34
db.users.remove{
    {age: {$gt: 30}, age: {$lt: 34}},
    {
        justOne: false
    }
}

Xóa những người có tuổi lớn hơn 30 và nhỏ hơn 34
db.users.remove{
    {age: {$in: ["Falcon","MongoDB"]}},
    {
        justOne: false
    }
}


Respectively!
Kiet Tram
From Vietnam

Comments