Mongodb

Tools

VSCode: cosmoDB https://code.visualstudio.com/docs/azure/mongodb

MongoDBCompass (cannot insert command)

Robot3T

Getting Started

Installation ( For MAC)

install environment

sudo mongod --dbpath /data/db/ . ## it needs write permission.

install driver

npm install --save mongodb

Initial Code

//init MongoClient and remember dbInstance at global scope then do those things needs database.
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';

MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");
  dbInstance = db;
  doSomeThingNeedsDb();
});

Use option upsert is convenient for achieving insertOrUpdate mechanism.

//update(query, data, options, cb)
const data = {
    key: 1,
    value: 'i am value'
};

const dataDb = db.getCollections('data');
dataDb.update({key:1}, data, {upsert:true}, function(err, res){

})

Cursor

avoid calling findOne multiple time, use Cursor.each instead.

find(query).each((err, doc)=>{ 
    console.log('your document here', err, doc);
})

Reference

http://mongodb.github.io/node-mongodb-native/2.2/quick-start/quick-start/

Shard

  • How to specify Shared Key

    • Range

      • Increase opportunity to find thhe single Shard that holds the required dataset.

    • Hashed

      • Needs to scatter the queries to all shards and find the dataset

Schema Design

Mongoose

  • Plugins

  • Schema

    • Model is useful when scale out the applications

  • Populate

    • Get the reference content by populate the field of reference Id

    • Or can use dynamic reference type to reference content in runtime,

    • the Calendar Model contains field of reference

    ```

CalendarEvent.find(deviceEventQuery).populate("reference");

eventType: { type: String, required: true, enum: EVENT_TYPES }, title: { type: String, required: true }, reference: { type: ObjectId, //reference to Device / PlayList EventSchema required: true, refPath: "eventType" }, ```

  • Operators $in, $gte, $lte, $gt, $lt, $contains $nin

  • conn3] update mybigday-restaurant-reservation-services.dinningphasereservations 
    query: { _id: ObjectId('5a326fc22e223100194684e9') } planSummary: 
    IDHACK update: { $pushAll: { reservation_list: [ { _id: ObjectId('5a32a24fee70c600199b4fbb'), 
    dinning_datetime: new Date(1513665000000), last_updated_datetime: new Date(1511250323000), 
    booking_method: { id: "8279273", source: "eztable_phone" }, 
    remark: "", statistics: { vegetarian_quantity: 0, baby_seat_quantity: 0, child_quantity: 0, people_quantity: 2, seat_quantity: 0 }, 
    time: { new_datetime: new Date(1513267791791) }, payment: { prepay_product: "", prepay_value: 0, prepay_amount: 0, pay_amount: 0, paid: false }, table_index_list: [], status: "confirmed", contact_info: { line_id: "", address: "", email: "", cellphone: "+886989452785", name: "" }, name: "紀x" }, 
    { _id: ObjectId('5a32a250ee70c600199b4fdf'), dinning_datetime: new Date(1513665000000), 
    last_updated_datetime: new Date(1510810259000),
    booking_method: { id: "8264863", source: "eztable_phone" }, 
    remark: " 生日*1", statistics: { vegetarian_quantity: 0, baby_seat_quantity: 0, child_quantity: 0, people_quantity: 3, seat_quantity: 0 }, 
    time: { new_datetime: new Date(1513267792690) }, payment: { prepay_product: "", prepay_value: 0, prepay_amount: 0, pay_amount: 0, paid: false }, table_index_list: [], status: "confirmed", contact_info: { line_id: "", address: "", email: "", cellphone: "+886953800302", name: "" }, name: "xxxx" } ] },
    $set: { statistics.available_reservation_seat_quantity: 546, statistics.people_quantity: 64 }, $inc: { __v: 1 } } keysExamined:1 docsExamined:1 nMatched:1 nModified:1 numYields:1 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2 } }, Collection: { acquireCount: { w: 2 } } } 118ms

https://docs.mongodb.com/manual/reference/database-profiler/

Performance

keyExamined less than nreturned

https://docs.mongodb.com/v3.4/reference/database-profiler/

Concurrency

Error

VersionError

Last updated

Was this helpful?