#MDBlocal
MongoDB Stitch Introduction
Drew DiPalma, Product Manager
#MDBlocal
When building, you need to…
1. Safely and easily access data
2. Integrate with key services
3. Scalably serve requests
#MDBlocal
MongoDB Query Language + Native DriversIntegrated Rules
Functions3rd Party Services
Native SDKs (JavaScript, Android, iOS)
Rest-like API
#MDBlocal
Integrated services and
Functions for complex,
connection logic
Native SDKs for Android,
JS, and iOS clients
Direct Database
Access
#MDBlocal
How requests work…
1. Application request is made
1
#MDBlocal
How requests work…
1. Application request is made
2. Stitch parses and applies rules
2
#MDBlocal
How requests work…
1. Application request is made
2. Stitch parses and applies rules
3. Stitch orchestrates DB + Services
3
3
#MDBlocal
How requests work…
1. Application request is made
2. Stitch parses and applies rules
3. Stitch orchestrates DB + Services
4. Stitch aggregates and applies rules
4
#MDBlocal
How requests work…
1. Application request is made
2. Stitch parses and applies rules
3. Stitch orchestrates DB + Services
4. Stitch aggregates and applies rules
5. Client receives results 5
const stitch = require('mongodb-stitch');
let client, mongodb, coll, users;
stitch.StitchClientFactory
.create('APP-ID')
.then(stitchClient => {
client = stitchClient;
mongodb = client.service('mongodb', 'mongodb-atlas');
coll = mongodb.db('Demo').collection(’Users');
});
MongoDB Services Functions
Stitch Concepts
client.login().then(() => {
// You can use client.authenticate() to access other auth providers
client.authenticate('providerType', {options})
// Providers include:
// - Anonymous
// - Email/Password
// - Facebook
// - Google
// - API Keys
// - Custom (signed JWT)
MongoDB Services Functions
Stitch Concepts
client.login().then(() => {
coll.find({'city': 'Seattle'}).execute().then(docs => {
users = docs;
});
});
// Or execute almost any find/aggregate command
coll.find({'city': 'Seattle'})
.aggregate([…])
.count(…)
.deleteMany(…)
.deleteOne(…)
.insertMany(…)
.insertOne(…)
.updateMany(…)
.updateOne(…)
MongoDB Services Functions
Stitch Concepts
const twilioService = client.service('twilio', 'twilioService')
twilioService.send('+120155555553', '+12018675309', message)
const sesService = client.service('ses', 'sesService')
sesService.send(fromEmail, toEmail, message)
const s3Service = client.service('s3', 's3Service')
s3Service.put(bucket, key, acl, contentType)
MongoDB Services Functions
Stitch Concepts
#MDBlocal
• Scalable, hosted JavaScript functions
• Written using ECMAScript 5
• Easily incorporate application context
- context.values
- context.services
- context.user
- context.request
- context.functions
- context.utils
Stitch Functions
client.executeFunction('sendWeather', 'Seattle');
MongoDB Services Functions
Stitch Concepts
client.executeFunction('sendWeather', 'Seattle');
MongoDB Services Functions
Stitch Concepts
//Function sendweather
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 = [];
for(var i in users){
statuses.push(twilio.send(context.values.get("twilioPhone")), users[i].Phone, forecast);
}
return statuses;
}
#MDBlocal
• Control DB/Service/Function access
• Fine-grained data access controls
• Define with simple JSON or link to
functions
• Associate with user profile, MDB Data,
or external info
Declarative Access Controls
#MDBlocal
Atlas
Customer Account Data
{
"owner_id": … ,
"balance": … ,
"transactions": {…},
"user_profile": {….}
}Bank
Teller
Analys
t
Custome
r
Roles,Permissions,andSecurity
Application
Reporting Data Mart
User Profile:
“I need to access my account”
Role Based:
“I need to lookup transactions
without seeing personal information”
Aggregate Only:
“I need to understand the bank’s total
cash flow”
#MDBlocal
Bank
Teller
Analys
t
Custome
r
Atlas
Customer Account Data
{
"owner_id": … ,
"balance": … ,
"transactions": {…},
"user_profile": {….}
}
Stitch
user_profile
(Read Rule)
{”owner_id": ”%%user.id"}
balance/transactions
(Read Rules)
{
”%or": [
{”owner_id": ”%%user.id"},
{"%%true": { "%function":
{ "name": ”isTeller",
"args": []
}
]
}
{
"user_id": … ,
"balance": … ,
"transactions":
{…},
"user_profile": {….}
}
#MDBlocal
Stitch
user_profile
(Read Rule)
{”owner_id": ”%%user.id"}
balance/transactions
(Read Rules)
{
”%or": [
{”owner_id": ”%%user.id"},
{"%%true": { "%function":
{ "name": ”isTeller",
"args": []
}
]
}
Bank
Teller
Analys
t
Custome
r
Atlas
Customer Account Data
{
"owner_id": … ,
"balance": … ,
"transactions": {…},
"user_profile": {….}
}
{
"user_id": … ,
"balance": … ,
"transactions": {…}
}
#MDBlocal
Stitch
user_profile
(Read Rule)
{”owner_id": ”%%user.id"}
balance/transactions
(Read Rules)
{
”%or": [
{”owner_id": ”%%user.id"},
{"%%true": { "%pipeline":
{ "name": "CheckAuth",
"args": { "AuthType": "server",
"UserAuth": "%%user.type"}
}
]
}
Stitch
user_profile
(Read Rule)
{”owner_id": ”%%user.id"}
balance/transactions
(Read Rules)
{
”%or": [
{”owner_id": ”%%user.id"},
{"%%true": { "%function":
{ "name": ”isTeller",
"args": []
}
]
}
Bank
Teller
Analys
t
Custome
r
{Aggregated
Data}
Stitch
Function for Analysis
{
"%%true": { "%function":
{ "name": ”isAnalyst",
"args": []}
}
Atlas
Customer Account Data
{
"owner_id": … ,
"balance": … ,
"transactions": {…},
"user_profile": {….}
}
#MDBlocal
Stitch in Action
#MDBlocal
Amazon S3 MongoDB
Atlas
twilio Amazon
Rekognition
2FA Access Control with Stitch
Secure IoT Client
• Bi-directional communication
• Secure com via API key auth
• Device access to DB,
functions and Services
Complex, Scalable Backend
• Deeply integrated services
• Coordination logic via
Functions
• Security through Auth + Rules
• Elastic scale
#MDBlocal
MongoDB
Atlas
Twilio
Amazon
Rekognition
{key: “*”}
capture_image()
{match: Drew,
Prob: 99.0%}
{user: “Drew”,
Ph#: 555-1212}
{user: Drew,
OTP: 135}
{user: Drew,
OTP: 135}
Ph#: 555-1212
{Ph#: 555-1212,
OTP: 135}
{OTP: 135}
{OTP: 135}
Amazon S3
process:
1. User requests photo + SMS
entry
2. Stitch asks ElectricImp for
photo
3. EI takes photo, sends to
Stitch
4. Stitch sends the photo to
AWS S3 and Rekognition
5. Rekognition sends image
analysis
6. Stitch queries Atlas for User
7. Stitch generates an entry
code, writes log, sends text
via Twilio
8. User enters code with key
pad, ElectricImp sends to
Stitch
9. Stitch checks code against
logs
10. If the code is valid, the Stitch
sends Electric Imp a message
to unlock.
#MDBlocal
Stitch Demo
#MDBlocal
Add Features
• Take your app to a
new platform
• Build a better
permissions
system or a
separate admin
portal
• Add additional
authentication
providers
Expose Data
• Provide an API for
safe, internal data
access
• Build more precise
permissions
system
• Create a real-time
dashboard with
authentication and
security built-in
Integrate Services
• Integrate with any
service or API with
minimal code
• Consume/action on
data in real time
with webhooks
• Communicate with
data using text,
e-mail, etc.
Complete
Backend
• Build faster without
boilerplate code
• Easily span multiple
platforms
• Scale at a moments
notice
• Single, simplified
point to manage
infrastructure/logic
#MDBlocal
• $1 per GB of data transferred from Stitch to client/service
• 25 GB Free per month
• Data transfer to Atlas is free
Pricing
#MDBlocal
xx
Everywher
e
Expand Regional footprint
Available on-premise
Bring any MongoDB
Realtime
Change Streams
driven
Event-based functions
Improve
Dev
App Import/Export Tools
User/Log Management
Rules Usability
What’s Next?
#MDBlocal
• Check out our hands-on lab: 12:50PM in the Sound Room
• Try out 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 Now?
#MDBlocal
THANK YOU
FOR JOINING!

Building Your First App with MongoDB Stitch

  • 1.
  • 2.
    #MDBlocal When building, youneed to… 1. Safely and easily access data 2. Integrate with key services 3. Scalably serve requests
  • 3.
    #MDBlocal MongoDB Query Language+ Native DriversIntegrated Rules Functions3rd Party Services Native SDKs (JavaScript, Android, iOS) Rest-like API
  • 4.
    #MDBlocal Integrated services and Functionsfor complex, connection logic Native SDKs for Android, JS, and iOS clients Direct Database Access
  • 5.
    #MDBlocal How requests work… 1.Application request is made 1
  • 6.
    #MDBlocal How requests work… 1.Application request is made 2. Stitch parses and applies rules 2
  • 7.
    #MDBlocal How requests work… 1.Application request is made 2. Stitch parses and applies rules 3. Stitch orchestrates DB + Services 3 3
  • 8.
    #MDBlocal How requests work… 1.Application request is made 2. Stitch parses and applies rules 3. Stitch orchestrates DB + Services 4. Stitch aggregates and applies rules 4
  • 9.
    #MDBlocal How requests work… 1.Application request is made 2. Stitch parses and applies rules 3. Stitch orchestrates DB + Services 4. Stitch aggregates and applies rules 5. Client receives results 5
  • 10.
    const stitch =require('mongodb-stitch'); let client, mongodb, coll, users; stitch.StitchClientFactory .create('APP-ID') .then(stitchClient => { client = stitchClient; mongodb = client.service('mongodb', 'mongodb-atlas'); coll = mongodb.db('Demo').collection(’Users'); }); MongoDB Services Functions Stitch Concepts
  • 11.
    client.login().then(() => { //You can use client.authenticate() to access other auth providers client.authenticate('providerType', {options}) // Providers include: // - Anonymous // - Email/Password // - Facebook // - Google // - API Keys // - Custom (signed JWT) MongoDB Services Functions Stitch Concepts
  • 12.
    client.login().then(() => { coll.find({'city':'Seattle'}).execute().then(docs => { users = docs; }); }); // Or execute almost any find/aggregate command coll.find({'city': 'Seattle'}) .aggregate([…]) .count(…) .deleteMany(…) .deleteOne(…) .insertMany(…) .insertOne(…) .updateMany(…) .updateOne(…) MongoDB Services Functions Stitch Concepts
  • 13.
    const twilioService =client.service('twilio', 'twilioService') twilioService.send('+120155555553', '+12018675309', message) const sesService = client.service('ses', 'sesService') sesService.send(fromEmail, toEmail, message) const s3Service = client.service('s3', 's3Service') s3Service.put(bucket, key, acl, contentType) MongoDB Services Functions Stitch Concepts
  • 14.
    #MDBlocal • Scalable, hostedJavaScript functions • Written using ECMAScript 5 • Easily incorporate application context - context.values - context.services - context.user - context.request - context.functions - context.utils Stitch Functions
  • 15.
  • 16.
    client.executeFunction('sendWeather', 'Seattle'); MongoDB ServicesFunctions Stitch Concepts //Function sendweather 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 = []; for(var i in users){ statuses.push(twilio.send(context.values.get("twilioPhone")), users[i].Phone, forecast); } return statuses; }
  • 17.
    #MDBlocal • Control DB/Service/Functionaccess • Fine-grained data access controls • Define with simple JSON or link to functions • Associate with user profile, MDB Data, or external info Declarative Access Controls
  • 18.
    #MDBlocal Atlas Customer Account Data { "owner_id":… , "balance": … , "transactions": {…}, "user_profile": {….} }Bank Teller Analys t Custome r Roles,Permissions,andSecurity Application Reporting Data Mart User Profile: “I need to access my account” Role Based: “I need to lookup transactions without seeing personal information” Aggregate Only: “I need to understand the bank’s total cash flow”
  • 19.
    #MDBlocal Bank Teller Analys t Custome r Atlas Customer Account Data { "owner_id":… , "balance": … , "transactions": {…}, "user_profile": {….} } Stitch user_profile (Read Rule) {”owner_id": ”%%user.id"} balance/transactions (Read Rules) { ”%or": [ {”owner_id": ”%%user.id"}, {"%%true": { "%function": { "name": ”isTeller", "args": [] } ] } { "user_id": … , "balance": … , "transactions": {…}, "user_profile": {….} }
  • 20.
    #MDBlocal Stitch user_profile (Read Rule) {”owner_id": ”%%user.id"} balance/transactions (ReadRules) { ”%or": [ {”owner_id": ”%%user.id"}, {"%%true": { "%function": { "name": ”isTeller", "args": [] } ] } Bank Teller Analys t Custome r Atlas Customer Account Data { "owner_id": … , "balance": … , "transactions": {…}, "user_profile": {….} } { "user_id": … , "balance": … , "transactions": {…} }
  • 21.
    #MDBlocal Stitch user_profile (Read Rule) {”owner_id": ”%%user.id"} balance/transactions (ReadRules) { ”%or": [ {”owner_id": ”%%user.id"}, {"%%true": { "%pipeline": { "name": "CheckAuth", "args": { "AuthType": "server", "UserAuth": "%%user.type"} } ] } Stitch user_profile (Read Rule) {”owner_id": ”%%user.id"} balance/transactions (Read Rules) { ”%or": [ {”owner_id": ”%%user.id"}, {"%%true": { "%function": { "name": ”isTeller", "args": [] } ] } Bank Teller Analys t Custome r {Aggregated Data} Stitch Function for Analysis { "%%true": { "%function": { "name": ”isAnalyst", "args": []} } Atlas Customer Account Data { "owner_id": … , "balance": … , "transactions": {…}, "user_profile": {….} }
  • 22.
  • 23.
    #MDBlocal Amazon S3 MongoDB Atlas twilioAmazon Rekognition 2FA Access Control with Stitch Secure IoT Client • Bi-directional communication • Secure com via API key auth • Device access to DB, functions and Services Complex, Scalable Backend • Deeply integrated services • Coordination logic via Functions • Security through Auth + Rules • Elastic scale
  • 24.
    #MDBlocal MongoDB Atlas Twilio Amazon Rekognition {key: “*”} capture_image() {match: Drew, Prob:99.0%} {user: “Drew”, Ph#: 555-1212} {user: Drew, OTP: 135} {user: Drew, OTP: 135} Ph#: 555-1212 {Ph#: 555-1212, OTP: 135} {OTP: 135} {OTP: 135} Amazon S3 process: 1. User requests photo + SMS entry 2. Stitch asks ElectricImp for photo 3. EI takes photo, sends to Stitch 4. Stitch sends the photo to AWS S3 and Rekognition 5. Rekognition sends image analysis 6. Stitch queries Atlas for User 7. Stitch generates an entry code, writes log, sends text via Twilio 8. User enters code with key pad, ElectricImp sends to Stitch 9. Stitch checks code against logs 10. If the code is valid, the Stitch sends Electric Imp a message to unlock.
  • 25.
  • 26.
    #MDBlocal Add Features • Takeyour app to a new platform • Build a better permissions system or a separate admin portal • Add additional authentication providers Expose Data • Provide an API for safe, internal data access • Build more precise permissions system • Create a real-time dashboard with authentication and security built-in Integrate Services • Integrate with any service or API with minimal code • Consume/action on data in real time with webhooks • Communicate with data using text, e-mail, etc. Complete Backend • Build faster without boilerplate code • Easily span multiple platforms • Scale at a moments notice • Single, simplified point to manage infrastructure/logic
  • 27.
    #MDBlocal • $1 perGB of data transferred from Stitch to client/service • 25 GB Free per month • Data transfer to Atlas is free Pricing
  • 28.
    #MDBlocal xx Everywher e Expand Regional footprint Availableon-premise Bring any MongoDB Realtime Change Streams driven Event-based functions Improve Dev App Import/Export Tools User/Log Management Rules Usability What’s Next?
  • 29.
    #MDBlocal • Check outour hands-on lab: 12:50PM in the Sound Room • Try out 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 Now?
  • 30.