You have valuable data in MongoDB and while it's important to use that data to empower your users and customers it can be tough to do so in a safe, secure way. In this session, you'll learn how to simply connect your users with the data they need using MongoDB Stitch.
2. Rendering
Data
App ServerDatabase IoT DevicesPhone
Browser
State
Logic
CRUD Email
QueuesAccess Control
Payments
State
Sign On
Service
Coordination
Images
Applications are evolving
3. Rendering
Data
App ServerDatabase IoT DevicesPhone
Browser
State
Logic
CRUD Email
QueuesAccess Control
Payments
State
Sign On
Service
Coordination
Images
Applications are evolving
5. How is development evolving?
Application are living in more locations:
• 48% of apps have backends deployed to a public cloud
• 58% of apps are now developed for multiple platforms
More is expected of development:
• 64% of developers identified as full-stack (2017)
Too much time is spent on maintaining:
• 41% of time spent maintaining
• 39% on new projects
Deploy and scale your application seamlessly
Write straightforward, standard code
Build simple, secure integrations
6. Traditional Applications….
• Contain many, separate layers
• Each layer is separate code, infrastructure
• Connecting/Maintaining layers is lots of work
Building an Application with Stitch
7. Traditional Applications….
• Contain many, separate layers
• Each layer is separate code, infrastructure
• Connecting/Maintaining layers is lots of work
Building an Application with Stitch
Security
&
Auth
Integrations
Business Logic
Data Access
8. Traditional Applications….
• Contain many, separate layers
• Each layer is separate code, infrastructure
• Connecting/Maintaining layers is lots of work
Building an Application with Stitch
Building applications with Stitch....
• No infrastructure to scale/patch/manage
• Simplified service integrations
• Easy to configure security
• Hosted, serverless business logic
9. Stitch is comprised of 4 services –
QueryAnywhere
Brings MongoDB's rich
query language safely to
your application
Build full apps for iOS,
Android, Web, and IoT
Functions
Integrate microservices +
server-side logic + cloud
services
Power apps with Server-side
logic, or enable Data as a
Service with custom APIs.
Triggers
Real-time notifications let your
application functions react in
response to database changes
App responds immediately to
changes
Mobile Sync
Automatically synchronizes
data between documents
held locally in MongoDB
Mobile and your backend
database
(Coming Soon) (Beta)
10. Apps with MongoDB Stitch
Cloud Infrastructure
Access Rules & Service Integrations
Application Logic (Functions/Triggers)
MongoDB Atlas
Rapidly deploy, dynamically scale, and distribute
databases across regions and cloud providers
MongoDB Stitch
Serverless platform with integrated authentication,
access rules, functions, and service integrations.
Client Application or Service
Application Logic
Local Application DataMongoDB Mobile Local MongoDB optimized for devices
MongoDB Stitch SDKs
Native SDKs with integrated authentication, and
providing access to Stitch, Atlas, and integrated
Services.
12. MongoDB Query Language + Native DriversIntegrated Rules
Functions3rd Party Services
Native SDKs (JavaScript, Android, iOS)
Rest API
How Applications interact with Stitch –
16. Filters in Stitch
User
{
"name": "userId",
"apply_when": {"%%true" : "%%true"},
"query": {"userid":"%%user.id"}
}
Filters contain an ApplyWhen and a
Query which is appended to a request
db.collection.find()
(plus user info)
17. Filters in Stitch
User
db.collection.find({userid: "%%user.id"})
{
"name": "userId",
"apply_when": {"%%true" : "%%true"},
"query": {"userid":"%%user.id"}
}
Filters contain an ApplyWhen and a
Query which is appended to a request
18. Filters in Stitch
User
db.collection.find({userid: "5b2..."})
{
"name": "userId",
"apply_when": {"%%true" : "%%true"},
"query": {"userid":"%%user.id"}
}
Filters contain an ApplyWhen and a
Query which is appended to a request
19. Filters in Stitch
User
db.collection.find({userid: "5b2..."})
{
"name": "userId",
"apply_when": {"%%true" : "%%true"},
"query": {"userid":"%%user.id"}
}
Filters contain an ApplyWhen and a
Query which is appended to a request
20. Filters in Stitch
User Results
{
"name": "userId",
"apply_when": {"%%true" : "%%true"},
"query": {"userid":"%%user.id"}
}
Filters contain an ApplyWhen and a
Query which is appended to a request
21. Filters in Stitch
User [{
userid: "5b2..."
data: …
isSecret: false
},{
userid: "5b2..."
data: …
isSecret: true
}]
22. [{
userid: "5b2..."
data: …
isSecret: false
},{
userid: "5b2..."
data: …
isSecret: true
}]
Roles are defined by Match statements,
evaluated per document, and assign a set of
Rules per document
User
Roles in Stitch
23. [{
userid: "5b2..."
data: …
isSecret: false
},{
userid: "5b2..."
data: …
isSecret: true
}]
Roles are defined by Match statements,
evaluated per document, and assign a set of
Rules per document
User
Roles in Stitch
24. Roles are defined by Match statements,
evaluated per document, and assign a set of
Rules per document
User
{name: "Not Secret Data",
apply_when: {"isSecret": false},
fields: {…}
}
Roles in Stitch
25. Roles are defined by Match statements,
evaluated per document, and assign a set of
Rules per document
User
{name: "Not Secret Data",
apply_when: {"isSecret": false},
fields: {…}
}
[{
userid: "5b2..."
data: …
isSecret: false
}]
Roles in Stitch
27. User
Rules in Stitch
"fields": {
"data": {"read": true}
},
"additional_fields": {
"read": false,
"write": false
}
Each Role has a set of matching Rules that
define read and write access at the field-level
[{
userid: "5b2..."
data: …
isSecret: false
}]
28. User
Rules in Stitch
"fields": {
"data": {"read": true}
},
"additional_fields": {
"read": false,
"write": false
}
Each Role has a set of matching Rules that
define read and write access at the field-level
29. User
Rules in Stitch Each Role has a set of matching Rules that
define read and write access at the field-level
[{
data: …
}]
30. User
Rules in Stitch Each Role has a set of matching Rules that
define read and write access at the field-level
[{
data: …
}]
32. schema: {
bsonType: "object",
required: ["userid", "data", "isSecret"],
properties: {
userid: {
bsonType: "objectid",
description: "The ID of the Stitch user"
},
data: {
bsonType: "string",
description: "must be a string and is not required"
},
isSecret: {
bsonType: "bool",
description: "True if the data is a secret"
}
additionalProperties: false
}
JSON Schema
33. schema: {
bsonType: "object",
required: ["userid", "data", "isSecret"],
properties: {
userid: {
bsonType: "objectid",
description: "The ID of the Stitch user"
},
data: {
bsonType: "string",
validate: {"%%true": {"%function": {
"name": "isValid",
"arguments": ["%%user", "%%this"]}}}
description: "must be a string and is not required"
},
isSecret: {
bsonType: "bool",
description: "True if the data is a secret"
}
[...]
Schema Validation
35. # M D B l o c a l
const stitchClient = stitch.initializeDefaultAppClient('myApp');
MongoDB Services Functions
Stitch Concepts
36. # M D B l o c a l
const stitchClient = stitch.initializeDefaultAppClient('myApp');
// Connect to a MongoDB Atlas database
const coll = stitchClient
.getServiceClient(RemoteMongoClient.factory, 'mongodb-atlas')
.db('Demo')
.collection('Users');
MongoDB Services Functions
Stitch Concepts
37. # M D B l o c a lMongoDB Services Functions
Stitch Concepts
38. # M D B l o c a l
// Authenticate, then add and retrieve documents.
stitchClient.auth.loginWithCredential(
MongoDB Services Functions
Stitch Concepts
39. # M D B l o c a l
// Authenticate, then add and retrieve documents.
stitchClient.auth.loginWithCredential(
// You can use loginWithCredential() to access any auth provider
// Providers include:
// - Anonymous
// - Email/Password
// - Facebook
// - Google
// - API Keys
// - Custom (signed JWT)
MongoDB Services Functions
Stitch Concepts
40. # M D B l o c a l
// Authenticate, then query the database
stitchClient.auth.loginWithCredential(new AnonymousCredential())
.then(() =>
coll.find().asArray()
).then(users =>
...
);
MongoDB Services Functions
Stitch Concepts
41. # M D B l o c a l
// Authenticate, then query the database
stitchClient.auth.loginWithCredential(new AnonymousCredential())
.then(() =>
coll.find().asArray()
).then(users =>
...
);
// Or execute almost any CRUD/Aggregate command
coll.find({})
.aggregate([…])
.count(…)
.deleteMany(…) / .deleteOne(…)
.insertMany(…) / .insertOne(…)
.updateMany(…) / .updateOne(…)
MongoDB Services Functions
Stitch Concepts
42. # M D B l o c a lMongoDB Services Functions
Stitch Concepts
43. # M D B l o c a l
const twilio = stitchClient.getServiceClient(
TwilioServiceClient.factory, 'myTwilioService');
MongoDB Services Functions
Stitch Concepts
44. # M D B l o c a l
const twilio = stitchClient.getServiceClient(
TwilioServiceClient.factory, 'myTwilioService');
twilio.sendMessage(toPhone, fromPhone, 'A message from Stitch!')
.then(() => console.log('success!’));
MongoDB Services Functions
Stitch Concepts
45. # M D B l o c a lMongoDB Services Functions
Stitch Concepts
46. # M D B l o c a l
const aws = stitchClient.getServiceClient(
AwsServiceClient.factory, 'awsService');
MongoDB Services Functions
Stitch Concepts
47. # M D B l o c a l
const aws = stitchClient.getServiceClient(
AwsServiceClient.factory, 'awsService');
const request = new AwsRequest.Builder()
.withService('s3')
.withAction('PutObject')
.withArgs(args)
.build();
MongoDB Services Functions
Stitch Concepts
48. # M D B l o c a l
const aws = stitchClient.getServiceClient(
AwsServiceClient.factory, 'awsService');
const request = new AwsRequest.Builder()
.withService('s3')
.withAction('PutObject')
.withArgs(args)
.build();
aws.execute(request).then(() => console.log('success!'));
MongoDB Services Functions
Stitch Concepts
50. Stitch Functions
Stitch is a collection of servers that
process application requests
Requests:
• Single actions for Database or Services
• Or executing a Stitch Function
• Integrated with Stitch’s rules
Functions:
• Scalable, hosted JavaScript (ES6) Functions
• Optimized for low-latency application requests
• Integrated with application context
• User, Request, Services, Values, etc.
Stitch Functions
addtoCart calcStats
… sendMail
Application
(Stitch SDK)
MongoDB
51. # M D B l o c a l
client.callFunction('sendWeather', ['London']);
MongoDB Services Functions
Stitch Concepts
52. # M D B l o c a l
client.callFunction('sendWeather', ['London']);
MongoDB Services Functions
Stitch Concepts
exports = function(city) {
var mongodb = context.services.get("mongodb-atlas");
var UserColl = mongodb.db("StitchDemo").collection("Users");
var twilio = context.services.get("twilio");
var weather = context.services.get("weatherAPI");
var users = UserColl.find({"city": city});
var forecast = weather.get({context.values.get("weatherURL") + city});
var statuses = [];
return Promise.all(users, forecast).then(data => {
for(var i in data.users){
statuses.push(twilio.send(context.values.get("twilio")), data.users[i].Phone, data.forecast);
}
return Promise.all(statuses);
});
}
53. # M D B l o c a l
client.callFunction('sendWeather', ['London']);
MongoDB Services Functions
Stitch Concepts
exports = function(city) {
var mongodb = context.services.get("mongodb-atlas");
var UserColl = mongodb.db("StitchDemo").collection("Users");
var twilio = context.services.get("twilio");
var weather = context.services.get("weatherAPI");
var users = UserColl.find({"city": city});
var forecast = weather.get({context.values.get("weatherURL") + city});
var statuses = [];
return Promise.all(users, forecast).then(data => {
for(var i in data.users){
statuses.push(twilio.send(context.values.get("twilio")), data.users[i].Phone, data.forecast);
}
return Promise.all(statuses);
});
}
54. # M D B l o c a l
client.callFunction('sendWeather', ['London']);
MongoDB Services Functions
Stitch Concepts
exports = function(city) {
var mongodb = context.services.get("mongodb-atlas");
var UserColl = mongodb.db("StitchDemo").collection("Users");
var twilio = context.services.get("twilio");
var weather = context.services.get("weatherAPI");
var users = UserColl.find({"city": city});
var forecast = weather.get({context.values.get("weatherURL") + city});
var statuses = [];
return Promise.all(users, forecast).then(data => {
for(var i in data.users){
statuses.push(twilio.send(context.values.get("twilio")), data.users[i].Phone, data.forecast);
}
return Promise.all(statuses);
});
}
55. # M D B l o c a l
client.callFunction('sendWeather', ['London']);
MongoDB Services Functions
Stitch Concepts
exports = function(city) {
var mongodb = context.services.get("mongodb-atlas");
var UserColl = mongodb.db("StitchDemo").collection("Users");
var twilio = context.services.get("twilio");
var weather = context.services.get("weatherAPI");
var users = UserColl.find({"city": city});
var forecast = weather.get({context.values.get("weatherURL") + city});
var statuses = [];
return Promise.all(users, forecast).then(data => {
for(var i in data.users){
statuses.push(twilio.send(context.values.get("twilio")), data.users[i].Phone, data.forecast);
}
return Promise.all(statuses);
});
}
56. # M D B l o c a l
client.callFunction('sendWeather', ['London']);
MongoDB Services Functions
Stitch Concepts
exports = function(city) {
var mongodb = context.services.get("mongodb-atlas");
var UserColl = mongodb.db("StitchDemo").collection("Users");
var twilio = context.services.get("twilio");
var weather = context.services.get("weatherAPI");
var users = UserColl.find({"city": city});
var forecast = weather.get({context.values.get("weatherURL") + city});
var statuses = [];
return Promise.all(users, forecast).then(data => {
for(var i in data.users){
statuses.push(twilio.send(context.values.get("twilio")), data.users[i].Phone, data.forecast);
}
return Promise.all(statuses);
});
}
58. • Import/Export your application
configuration
• Develop with Stitch locally
• Add to repositories for versioning
and collaboration
• Easily move code between
environments
• Use structure from existing apps to
create something new
Stitch CLI & Import/Export
yourStitchApp/
├── stitch.json
├── auth_providers/
│ └── <provider name>.json
├── functions/
│ └── <function name>/
│ ├── config.json
│ └── source.js
├── services/
│ └── <service name>/
│ ├── config.json
│ ├── incoming_webhooks/
│ │ ├── config.json
│ │ └── source.js
│ └── rules/
│ └── <rule name>.json
| ...
59. Simple & Cheap Consumption Pricing
• Streamlined with Database (MongoDB Atlas)
Data Transfer
• $.5/GB Data Downloaded
• 25 GB Data free/month
• Transfer to Atlas is free
Compute
• $0.000025 per GB-s for Compute
• Runtime - Length of request in ms
• Memory - Max memory used in 10MB blocks
• First 1,000,000 functions or 100,000GB-s free
Pricing
60. • Check out another talk on Stitch –
• MongoDB Mobile: Bringing the Power of MongoDB to Your Device – 2:50pm – 3:30pm
• Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to the Next Level With Triggers – 4:45pm - 5:30pm
• Get started with Stitch – stitch.mongodb.com
• Check out SDKs and examples –
• Code at github.com/MongodbStitch
• Docs at docs.mongodb.com/stitch
• Build the Dashboard or Weather IoT apps in our Tutorials section
• Ask questions or let us know what you’re building through Intercom
What next?