What’s new in 2.6
DriverTeam Lead,Node.js driver,MongoDB INC
Christian Kvalheim
#mongodb
Me
• Driver Team Lead
• Node.js driver developer
• @christkv
• http://www.christiankvalheim.com
Agenda
• Aggregation Cursors
• maxTimeMS
• New or Enhanced update operations
• New Security Model
• Ordered/Unordered Bulk Operations
• Background index building on secondaries
• parallelCollectionScan command
Aggregation Cursors
Aggregation Cursors
• Aggregation framework can now return a cursor
var test = db.getSisterDB('test');!
test.t.drop();!
!
for(var i = 0; i < 100; i++) {!
db.t.insert({a:i});!
}!
!
var c = test.t.aggregate([{$match: {}}], {cursor: { batchSize:1 }});!
!
while(c.hasNext()) {!
print(c.next().a);!
}!
Aggregation Cursors
• You can control the behavior of the aggregation cursor
passing in the cursor option with the batchSize
> db.runCommand({aggregate: "t", pipeline: [], cursor: {batchSize: 1}!
! , allowDiskUse: true})!
{!
! "cursor" : {!
! ! "id" : NumberLong("39465949628"),!
! ! "ns" : "test.t",!
! ! "firstBatch" : [!
! ! ! {!
! ! ! ! "_id" : ObjectId("532808ea4be168cc8e9dd7dd"),!
! ! ! ! "a" : 0!
! ! ! }!
! ! ]!
! },!
! "ok" : 1!
}!
Aggregation Cursors
• Cursor id is just a normal MongoDB cursor meaning it
works like other cursors using the wire protocol call
getMore.
maxTimeMS
maxTimeMS
• Ever wanted to set a specific query or command
timeout ?
• maxTimeMS is what you’ve been looking for.
> db.t.find({"$where": "sleep(1000)"}).maxTimeMS(50)!
New or Enhanced update
operations
New or Enhanced update operations
• $mul
• $bit
• $min/$max
• $currentDate
• $push enhancements
$mul
var test = db.getSisterDB('test');!
test.t.drop();!
!
db.t.insert({ _id: 1, item: "ABC", price: 10.99 });!
!
db.t.update({ _id: 1 },!
{ $mul: { price: 1.25 } });!
!
print(db.t.findOne({_id:1}).price);!
$bit
• supports and/or/xor (xor is new)
var test = db.getSisterDB('test');!
test.t.drop();!
!
db.t.insert({ _id: 1, expdata: NumberLong(1) });!
!
db.t.update({ _id: 1 },!
{ $bit: { expdata: { xor:
NumberInt(5) } } })!
!
print(db.t.findOne({_id:1}).expdata);!
$min/$max
var test = db.getSisterDB('test');!
test.t.drop();!
!
db.t.insert({ _id: 1, desc: "crafts", !
dateEntered: ISODate("2013-10-01T05:00:00Z"),!
dateExpired: ISODate("2013-10-01T16:38:16Z")!
});!
!
db.t.update({ _id: 1 }, {!
$min: { dateEntered: new Date("2013-09-25") }!
})!
!
print(db.t.findOne({_id:1}).dateEntered);!
$currentDate
var test = db.getSisterDB('test');!
test.t.drop();!
!
db.t.insert({ _id: 1, !
! status: "a", !
! lastModified: ISODate("2013-10-02T01:11:18.965Z") });!
!
db.t.update({ _id: 1 }, {!
! $currentDate: {!
! ! lastModified: true,!
! ! lastModifiedTS: { $type: "timestamp" }},!
! $set: { status: "D" }!
});!
!
printjson(db.t.findOne({_id:1}));!
$push enhancements
var test = db.getSisterDB('test');!
test.t.drop();!
!
db.t.insert({ "_id" : 1, "scores" : [50,60,70,100 ]});!
!
db.t.update({ _id: 1 }, !
! { $push: { scores: {!
! ! ! $each: [ 20, 30 ],!
! ! ! $position: 2!
! ! }!
! }!
});!
!
printjson(db.t.findOne({_id:1}));!
New Security Model
New Security Model
• Now with
• Roles
• Rights
• You can customize the roles and rights to your liking.
• Subscription Edition also includes
• LDAP
• X509 authentication
New Security Model - Create Role Ex
var admin = db.getSisterDB('admin');!
admin.createRole({!
role: "myClusterwideAdmin",!
privileges:!
[!
{ resource: { cluster: true }, !
! actions: [ "addShard" ] },!
{ resource: { db: "config", collection: "" }, !
! actions: [ "find", "update", "insert" ] },!
{ resource: { db: "users", collection: "usersCollection" }, !
! actions: [ "update" ] },!
{ resource: { db: "", collection: "" }, !
! actions: [ "find" ] }!
],!
roles:!
[!
{ role: "read", db: "admin" }!
],!
writeConcern: { w: "majority" , wtimeout: 5000 }!
})!
Ordered/Unordered Bulk
Operations
Ordered/Unordered Bulk Operations
var test = db.getSisterDB('test');!
test.t.drop();!
!
var bulk = db.t.initializeOrderedBulkOp();!
bulk.insert({a:1});!
bulk.find({a:1}).update({$set: {b:1}});!
bulk.find({a:2}).upsert().update({$set: {b:1}});!
bulk.find({a:1}).remove();!
var result = bulk.execute({w:1});!
printjson(result);!
Ordered/Unordered Bulk Operations
• New BulkAPI shared across drivers and shell
• Uses new write Commands underneath the covers
• Splits up documents into batches
• Write Commands is the future of writing for MongoDB
• w:0 semantics is no result details when using write
commands
Ordered/Unordered Bulk Operations
• Ordered
• Execute all operations in the order they where
entered and fail on first write error
• Guarantees order of execution
• Unordered
• Executes all operations out of order and potentially
in parallel.Does not stop on error.
• Does not Guarantee order of execution
Background index building on
secondaries
Background index building on secondaries
• Previously only available on primary
• Could cause secondaries to hang
• Was not practical in many situations
• Secondaries can now build indexes in background
• Can also restart partially build indexes on instance
termination and restart
parallelCollectionScan command
parallelCollectionScan command
• Split collection into multiple cursors
• Read in parallel from the collection
• No matching semantics (it’s for pure dumping
purposes only)
parallelCollectionScan command
var test = db.getSisterDB('test');!
test.t.drop();!
!
var bulk = db.t.initializeOrderedBulkOp();!
for(var i = 0; i < 100000; i++) {!
! bulk.insert({a:i});!
};!
!
var result = bulk.execute({w:1});!
result = test.runCommand({!
! ! parallelCollectionScan: 't'!
! ,! numCursors: 4!
});!
!
printjson(result);!
ThankYou
DriverTeam Lead,Node.js driver,MongoDB INC
Christian Kvalheim
#mongodb

New in MongoDB 2.6

  • 1.
    What’s new in2.6 DriverTeam Lead,Node.js driver,MongoDB INC Christian Kvalheim #mongodb
  • 2.
    Me • Driver TeamLead • Node.js driver developer • @christkv • http://www.christiankvalheim.com
  • 3.
    Agenda • Aggregation Cursors •maxTimeMS • New or Enhanced update operations • New Security Model • Ordered/Unordered Bulk Operations • Background index building on secondaries • parallelCollectionScan command
  • 4.
  • 5.
    Aggregation Cursors • Aggregationframework can now return a cursor var test = db.getSisterDB('test');! test.t.drop();! ! for(var i = 0; i < 100; i++) {! db.t.insert({a:i});! }! ! var c = test.t.aggregate([{$match: {}}], {cursor: { batchSize:1 }});! ! while(c.hasNext()) {! print(c.next().a);! }!
  • 6.
    Aggregation Cursors • Youcan control the behavior of the aggregation cursor passing in the cursor option with the batchSize > db.runCommand({aggregate: "t", pipeline: [], cursor: {batchSize: 1}! ! , allowDiskUse: true})! {! ! "cursor" : {! ! ! "id" : NumberLong("39465949628"),! ! ! "ns" : "test.t",! ! ! "firstBatch" : [! ! ! ! {! ! ! ! ! "_id" : ObjectId("532808ea4be168cc8e9dd7dd"),! ! ! ! ! "a" : 0! ! ! ! }! ! ! ]! ! },! ! "ok" : 1! }!
  • 7.
    Aggregation Cursors • Cursorid is just a normal MongoDB cursor meaning it works like other cursors using the wire protocol call getMore.
  • 8.
  • 9.
    maxTimeMS • Ever wantedto set a specific query or command timeout ? • maxTimeMS is what you’ve been looking for. > db.t.find({"$where": "sleep(1000)"}).maxTimeMS(50)!
  • 10.
    New or Enhancedupdate operations
  • 11.
    New or Enhancedupdate operations • $mul • $bit • $min/$max • $currentDate • $push enhancements
  • 12.
    $mul var test =db.getSisterDB('test');! test.t.drop();! ! db.t.insert({ _id: 1, item: "ABC", price: 10.99 });! ! db.t.update({ _id: 1 },! { $mul: { price: 1.25 } });! ! print(db.t.findOne({_id:1}).price);!
  • 13.
    $bit • supports and/or/xor(xor is new) var test = db.getSisterDB('test');! test.t.drop();! ! db.t.insert({ _id: 1, expdata: NumberLong(1) });! ! db.t.update({ _id: 1 },! { $bit: { expdata: { xor: NumberInt(5) } } })! ! print(db.t.findOne({_id:1}).expdata);!
  • 14.
    $min/$max var test =db.getSisterDB('test');! test.t.drop();! ! db.t.insert({ _id: 1, desc: "crafts", ! dateEntered: ISODate("2013-10-01T05:00:00Z"),! dateExpired: ISODate("2013-10-01T16:38:16Z")! });! ! db.t.update({ _id: 1 }, {! $min: { dateEntered: new Date("2013-09-25") }! })! ! print(db.t.findOne({_id:1}).dateEntered);!
  • 15.
    $currentDate var test =db.getSisterDB('test');! test.t.drop();! ! db.t.insert({ _id: 1, ! ! status: "a", ! ! lastModified: ISODate("2013-10-02T01:11:18.965Z") });! ! db.t.update({ _id: 1 }, {! ! $currentDate: {! ! ! lastModified: true,! ! ! lastModifiedTS: { $type: "timestamp" }},! ! $set: { status: "D" }! });! ! printjson(db.t.findOne({_id:1}));!
  • 16.
    $push enhancements var test= db.getSisterDB('test');! test.t.drop();! ! db.t.insert({ "_id" : 1, "scores" : [50,60,70,100 ]});! ! db.t.update({ _id: 1 }, ! ! { $push: { scores: {! ! ! ! $each: [ 20, 30 ],! ! ! ! $position: 2! ! ! }! ! }! });! ! printjson(db.t.findOne({_id:1}));!
  • 17.
  • 18.
    New Security Model •Now with • Roles • Rights • You can customize the roles and rights to your liking. • Subscription Edition also includes • LDAP • X509 authentication
  • 19.
    New Security Model- Create Role Ex var admin = db.getSisterDB('admin');! admin.createRole({! role: "myClusterwideAdmin",! privileges:! [! { resource: { cluster: true }, ! ! actions: [ "addShard" ] },! { resource: { db: "config", collection: "" }, ! ! actions: [ "find", "update", "insert" ] },! { resource: { db: "users", collection: "usersCollection" }, ! ! actions: [ "update" ] },! { resource: { db: "", collection: "" }, ! ! actions: [ "find" ] }! ],! roles:! [! { role: "read", db: "admin" }! ],! writeConcern: { w: "majority" , wtimeout: 5000 }! })!
  • 20.
  • 21.
    Ordered/Unordered Bulk Operations vartest = db.getSisterDB('test');! test.t.drop();! ! var bulk = db.t.initializeOrderedBulkOp();! bulk.insert({a:1});! bulk.find({a:1}).update({$set: {b:1}});! bulk.find({a:2}).upsert().update({$set: {b:1}});! bulk.find({a:1}).remove();! var result = bulk.execute({w:1});! printjson(result);!
  • 22.
    Ordered/Unordered Bulk Operations •New BulkAPI shared across drivers and shell • Uses new write Commands underneath the covers • Splits up documents into batches • Write Commands is the future of writing for MongoDB • w:0 semantics is no result details when using write commands
  • 23.
    Ordered/Unordered Bulk Operations •Ordered • Execute all operations in the order they where entered and fail on first write error • Guarantees order of execution • Unordered • Executes all operations out of order and potentially in parallel.Does not stop on error. • Does not Guarantee order of execution
  • 24.
  • 25.
    Background index buildingon secondaries • Previously only available on primary • Could cause secondaries to hang • Was not practical in many situations • Secondaries can now build indexes in background • Can also restart partially build indexes on instance termination and restart
  • 26.
  • 27.
    parallelCollectionScan command • Splitcollection into multiple cursors • Read in parallel from the collection • No matching semantics (it’s for pure dumping purposes only)
  • 28.
    parallelCollectionScan command var test= db.getSisterDB('test');! test.t.drop();! ! var bulk = db.t.initializeOrderedBulkOp();! for(var i = 0; i < 100000; i++) {! ! bulk.insert({a:i});! };! ! var result = bulk.execute({w:1});! result = test.runCommand({! ! ! parallelCollectionScan: 't'! ! ,! numCursors: 4! });! ! printjson(result);!
  • 29.
    ThankYou DriverTeam Lead,Node.js driver,MongoDBINC Christian Kvalheim #mongodb