Chuyển đến nội dung chính

Bài đăng

Đang hiển thị bài đăng từ Tháng 3, 2019

Các câu lệnh hay hữu ích trong MongoDB

1. forEach: - Sử dụng forEach để thay đổi giá trị các row: db.getCollection('CollectionA').find().forEach((x)=>{ x.value =x.value+1; print( x.value); db.getCollection('CollectionA').save(x); // lưu x với giá trị mới, nếu không có hàm save() thì sau khi chạy xong value sẽ như cũ. }) - Upsert row from Collection A to Collection B: db.getCollection('CollectionA').find().forEach((x)=>{  db.getCollection('CollectionB').update(    {"phone":x.phone},    {     "phone" : x.phone,     "name" : x.name,     "address" : "",     "email" : "",     "note" : "BH",     "describe" : x.describe,     "create_by" : {       "user_extension" : "101",       "username" : "101"     }   },   {    upsert: true  }  ) }) 2. FIND:   - Find dữ liệu with regex: -----db.getCollection(