6. # M D B l o c a l
const changeStream=
db.collection(‘temperature’).watch();
c h a n g e S t r e a m . o n ( ' c h a n g e ' , f u n c t i o n ( c h a n g e ) {
c o n s o l e . l o g ( c h a n g e ) ;
} ) ;
7. # M D B l o c a l
CHANGE STREAM
CHARACTERISTICS
8. # M D B l o c a l
5 OPERATION TYPES
INSERT
DELETE
REPLACE
UPDATE
INVALIDATE
9. # M D B l o c a l
CHANGE STREAMS UTILIZE
COLLECTION ACCESS CONTROLS,
PRESENT A DEFINED API, AND ENABLE
SCALING ACROSS PRIMARIES AND
SECONDARIES.
10. # M D B l o c a l
TOTAL ORDERING OF CHANGES ACROSS
SHARDS
mongos
3 1
2
Shard 1 Shard 2 Shard 3
12. # M D B l o c a l
SP
CHANGE STREAMS ARE RESUMABLE
P
S
{_id: <resumeToken>,
operationType: ‘update’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: ‘room12345’},
updateDescription: {updatedFields: {temperature: 70},
removedFields: [‘humidity’]}
}
var newChangeStream = coll.watch({ resumeAfter: <cachedResumeToken> });
13. # M D B l o c a l
CHANGE STREAMS UTILIZE THE POWER
OF THE AGGREGATION FRAMEWORK
$match $project $addFields $replaceRoot $redact
var changeStream = coll.watch([{ $match: {$or: [{operationType: 'delete' }, {operationType:'replace'}]}}]);
14. # M D B l o c a l
1. COLLECTION ACCESS CONTROLS
2. DEFINED API
3. ENABLE SCALING
4. TOTAL ORDERING
5. DURABLE
6. RESUMABLE
7. POWER OF AGGREGATION
17. # M D B l o c a l
{_id: (resumeToken),
operationType: ‘update’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘alice123’,
_id: ObjectId("58a4eb4a30c75625e00d2820")},
updateDescription: {updatedFields: {email: ‘alice@10gen.com’},
removedFields: [‘phoneNumber’]}
}
UPDATE
var changeStream = coll.watch({ fullDocument: 'updateLookup' });
UPDATE fullDocument: updateLookup
{_id: (resumeToken),
operationType: ‘update’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘alice123’,
_id: ObjectId("58a4eb4a30c75625e00d2820")},
updateDescription: {updatedFields: {email:
‘alice@10gen.com’},
removedFields: [‘phoneNumber’]},
fullDocument: { _id: ObjectId("58a4eb4a30c75625e00d2820"),
name: ‘Alice’,
userName: ‘alice123’,
email: ‘alice@10gen.com’,
team: ‘replication’}
}
18. # M D B l o c a l
1 . S H A R D K E Y I S U S E D ( B Y T H E M O N G O S ) TO R O U T E
O P E R AT I O N S
2 . _ I D U N I Q U E N E S S I S E N F O R C E D B Y S H A R D
mongos
3 1
2
Shard 1 Shard 2 Shard 3
DEFINING THE documentKey
Hey guys, thank you all for being here. I know it can get a little bit rough at this time at a full day event, so I appreciate you guys sticking with me. I promise you won’t regret it.
Today, I have the privilege of introducing Change Streams, a new feature in MongoDB 3.6 (being released later this year)
Before we get into the fun stuff, I have a little story.
I was at a trivia night recently and one of the questions was who popularized the term “time is money”. My guess, naturally, was Warren Buffet as the was the famous business man the popped into my head at the time. Well, turns out I was only a couple centuries off. The real answer is Benjamin Franklin. Benjamin Franklin popularized the term time is money in the1700s. What a fascinating phrase it feels weirdly modern doesn’t it? I think it’s because this phrase has never rang more true than it is today. With the evolution of technology, and enabled by these innovations, each of us is able to do more and more in every second. We live in an era where we can catch up on the lastest news, socialize with our friends, get a date for the evening -- all in the two minutes it takes for us to use the restroom. While that is a silly example, we have become accustomed to doing and reacting to things as quickly as possible.
As the builders we are in this room, we’ve realized that real-time is no longer this flashy nice-to-have in our applications. It is our users expectation. Whether those users are our bosses looking at real-time monitoring dashboard or the general public wanting updates on their food deliveries precise location. We at MongoDB sought out to make it easier for you to rise to the high expectations your users have. And with that, in MongoDB 3.6 we are introducing Change Streams.
In this talk, I will give you a simple definition of what change streams are and the power they possess. Then we will go into some one the charactertics change streams have. Finally, we will discuss how you can avoid some potential pitfalls.
So let’s start with a simple definition.
Change streams allow you to watch all the changes against a given collection. So, when you open change streams against a collection you will see all of the changes applied to documents that belong to that collection. On each change a listening application will be notified with a document describing the change.
Imagine this scenario, I have a smart thermometer within my apartment. Every second it inserts a document into MongoDB with temperature data. Change streams can then notify any listening application to every change in my temperature collection. So as the thermometer inserts new data, change streams will inform my listening action handler. In this case, the action handler is an application I built to turn on a fan every time the temperature goes above 70 degrees.
Don’t let the simplicity of this example fool you, this concept can be applied to back-office management applications to end user applications. Change streams are extremely powerful
So, this slide is to show you how incredibly simple it is to implement change streams in your application. This example happens to be in node.
Here, I am defining a change stream. Then starting it where every change gets printed out.
It’s extremely simple and incredibly powerful. With change stream, you get a real-time feed of the changes to your data, enabling your application to react to these changes immediately.
Now we are going to move into some of the characteristics of change streams.
Now we are going to move into some of the characteristics of change streams.
The oplog is an internal mechanism that tracks all changes across the entire cluster, whether they are data or system changes. The