Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB 3.2

MongoDB 3.2

What's hot and new in the latest MongoDB version: Document validation, $lookup, BI, storage engines, replicated config servers, and partial indexes.

Philipp Krenn

January 23, 2016
Tweet

More Decks by Philipp Krenn

Other Decks in Programming

Transcript

  1. Demo $ mkdir test $ mongod --dbpath test/ --port 27001

    --logpath test.log $ mongo --port 27001 > db.version() > db.contact.insert({ phone: "1234", email: "[email protected]", status: "ok" })
  2. Validation > db.createCollection("contact", { validator: { $and: [ { phone:

    { $type: "string" } }, { email: { $regex: /@test\.com$/ } }, { status: { $in: [ "ok", "incomplete" ] } } ] } } )
  3. Change to contacts and insert validation rule > db.getCollectionInfos() >

    db.contacts.insert({ phone: "1234", email: "[email protected]", status: "ok" }) > db.contacts.insert({ phone: "1234" }) > db.contacts.find()
  4. Update > db.runCommand({ collMod: "contacts", validator: { $or: [ {

    phone: { $type: "string" } }, { email: { $regex: /@test\.com$/ } }, { status: { $in: [ "ok", "incomplete" ] } } ] }, validationLevel: "strict", validationAction: "error" })
  5. Test > db.getCollectionInfos() > db.contacts.insert({ status: "ok" }) > db.contacts.insert({

    status: "foobar" }) > db.runCommand({ insert: "contacts", documents: [ { status: "foobar"} ], bypassDocumentValidation: true })
  6. Nested rules > db.runCommand({ collMod: "contacts", validator: { $or: [

    { $and: [ { phone: { $type: "string" } }, { email: { $regex: /@test\.com$/ } }, { status: "ok" } ] }, { status: "incomplete" } ] } }) > db.contacts.insert({ name: "philipp", status: "incomplete" })
  7. Versioning version is user-defined, type 2 is string > db.runCommand({

    collMod: "contacts", validator: { $or: [ { version: { "$exists": false } }, { version: 1, $and: [ { name: { "$exists": true } } ] }, { version: 2, $and: [ { name: { "$exists": true, "$type": 2 } } ] } ] } }) > db.contacts.insert({ test: 1 }) > db.contacts.insert({ name: 1, version: 1 }) > db.contacts.insert({ name: "philipp", version: 2 })
  8. Flags: validationLevel: strict | moderate | off If an existing

    document isn't valid, moderate updates won't validate validationAction: error | warn
  9. Limitations Only on insert and update No help why a

    validation failed $geoNear, $near, $nearSphere, $text, $where
  10. Some data > db.purchases.insert({ "buyer" : "bill", "item" : "macbook"

    }) > db.purchases.insert({ "buyer" : "fred", "item" : "macbook" }) > db.purchases.insert({ "buyer" : "john", "item" : "macbook" }) > db.purchases.insert({ "buyer" : "john", "item" : "thinkpad" })
  11. More data > db.products.insert({ name: "MacBook Pro", price: 2000, code:

    "macbook" }) > db.products.insert({ name: "Thinkpad", price: 1800, code: "thinkpad" })
  12. Lookup > db.purchases.aggregate([ { $group: { _id: "$item", total: {

    $sum: 1 } } }, { $lookup: { from: "products", localField: "_id", foreignField: "code", as: "item_details" } } ])
  13. Flattened lookup > db.products.aggregate([ { $lookup: { from: "purchases", localField:

    "code", foreignField: "item", as: "buyers" } }, { $unwind: "$buyers" } ])
  14. Flattened and cleaned lookup > db.products.aggregate([ { $lookup: { from:

    "purchases", localField: "code", foreignField: "item", as: "buyers" } }, { $unwind: "$buyers" }, { $project: { "_id": 0, "name": 1, "code": 1, "price": 1, "buyer": "$buyers.buyer" } } ])
  15. More aggregation functions $sample > db.mycoll.aggregate({ $sample: { size: 100

    } }) <5% data: Random cursor or index selection >=5% data: Scan entire collection, sort randomly in memory, fetch documents from the top
  16. Had MongoDB really done the impossible? Had they developed a

    connector which satisfies all the requirements of NoSQL analytics,...
  17. but exposes relational semantics on flat, uniform data, so legacy

    BI software can handle it? — https://www.linkedin.com/pulse/mongodb-32- now-powered-postgresql-john-de-goes
  18. MongoDB had gone from nothing to magic in just a

    few months [?] — https://www.linkedin.com/pulse/mongodb-32- now-powered-postgresql-john-de-goes
  19. It uses a foreign data wrapper with PostgreSQL to provide

    a relational SQL view into your MongoDB data. — https://docs.mongodb.org/manual/products/bi- connector/
  20. "Shit. This is bad news for MongoDB. Really bad." —

    https://www.linkedin.com/pulse/mongodb-32- now-powered-postgresql-john-de-goes
  21. Before 3.2 Exactly 3 independent config servers Read and write

    with 3 available nodes Read-only with less
  22. > db.users.insert({ username: "philipp", active: 1 }) > db.users.createIndex( {

    username: 1 }, { partialFilterExpression: { active: { $eq: 1 } } } ) > db.users.find({ username: "philipp", active: { $eq: 1} })
  23. > db.users.find({ username: "philipp", active: { $eq: 1} }).explain() {

    ... "winningPlan": { "stage": "FETCH", "filter": { "active": { "$eq": 1 } }, "inputStage": { "stage": "IXSCAN", "keyPattern": { "username": 1 }, ... > db.users.find({ username: "philipp" }).explain() { ... "winningPlan": { "stage": "COLLSCAN", "filter": { "username": { "$eq": "philipp" } }, ...