GIANT Stories at MongoDB

Working with MongoDB Stitch Through Existing Drivers – Python & PyMongo

Andrew Morgan

Technical, Cloud

You can now access MongoDB Stitch (and Atlas behind it) using any of the existing MongoDB Drivers – this post shows how to do it using the PyMongo Python Driver.

Working with MongoDB Stitch Through the mongo Shell – MongoDB Wire Protocol Support

Andrew Morgan

Technical, Cloud

The Stitch SDK is the best way to access MongoDB Stitch from your frontend application code – however, you may want to access Stitch from your existing tools (e.g. the mongo shell) or backend code – this post shows you how.

MongoDB Stitch Authentication Triggers

Andrew Morgan

Technical, Cloud

See how Stitch authentication triggers let you use third-party user-authentication services such as Facebook or Google without losing the ability to perform custom actions when users register, sign-in, or leave.

Sending Text Messages with MongoDB Stitch & Twilio

Andrew Morgan

Technical, Cloud

How to send text messages from your app using MongoDB Stitch and Twilio.

Testing & Debugging MongoDB Stitch Functions

Andrew Morgan

Technical, Cloud

Testing and debugging serverless functions can be tricky – not so with MongoDB Stitch functions. This post shows how quick and easy it is through the Stitch UI.

Building a REST API with MongoDB Stitch

Andrew Morgan

Technical, Cloud

MongoDB Stitch QueryAnywhere often removes the need to create REST APIs, but for the other times, Stitch webhooks let you create them in minutes.

Recording sensor data with MongoDB Stitch & Electric Imp

Andrew Morgan

Technical, Cloud

Electric Imp devices and cloud services are a great way to get started with IoT. Electric Imp's MongoDB Stitch client library makes it a breeze to integrate with Stitch. This post describes how.

Controlling humidity with a MongoDB Stitch HTTP service and IFTTT

Andrew Morgan

Technical, Cloud

IFTTT is a great cloud service for pairing up cloud and IoT services. This post shows how to invoke an IFTTT webhook from a MongoDB Stitch function, where that webhook controls a dehumidifier via a Smart power plug.

Working with the Stitch CLI – Editing Apps Outside of Stitch

Andrew Morgan

Technical

Introduction

This post introduces the MongoDB Stitch CLI, demonstrating how the Command Line Interface (CLI) can be used to export an existing app, continue to develop it locally, and then merge the changes back into Stitch. This workflow allows automation, enables collaboration, and increases productivity.

Introducing MongoDB Stitch

MongoDB Stitch is a serverless platform which accelerates application development with simple, secure access to data and services from the client – getting your apps to market faster while reducing operational costs and effort.

The Stitch serverless platform addresses the challenges faced by the developers of modern applications by providing four services:

  • Stitch QueryAnywhere: Exposes the full power of working with documents in MongoDB and the MongoDB query language, directly from your web and mobile application frontend code. A powerful rules engine lets developers declare fine-grained security policies.
  • Stitch Functions: Allows developers to run simple JavaScript functions in Stitch’s serverless environment, making it easy to create secure APIs or to build integrations with microservices and server-side logic. Enables integration with popular cloud services such as Slack and Twilio, enriching your apps with a single Stitch method call.
  • Stitch Triggers: Real-time notifications that launch functions in response to changes in the database. The functions can make further database changes, push data to other places, or interact with users – such as through push notifications, text messages, or emails.
  • Stitch Mobile Sync: Automatically synchronizes data between documents held locally in MongoDB Mobile and the backend database. MongoDB Mobile allows mobile developers to use the full power of MongoDB locally. Stitch Mobile Sync ensures that data is kept up to date across phones and all other clients in real time.

The Sitch CLI

Stitch has an intuitive UI to create and maintain your apps, but you also have the option of using the Stitch CLI. The stitch-cli is the key to automating workflows with Stitch (it also lets you develop code inside your favorite editor). The CLI provides functions for authenticating Stitch users, importing local application directories, and exporting applications from the MongoDB Stitch service.

With the import & export commands in the Stitch CLI, you can:

  • Develop Stitch applications using your favorite desktop editor and import them into Stitch
  • Export existing applications to a local directory. From there you can:
    • Add the files to source control (e.g., GitHub)
    • Make changes to the application code and configuration files, before merging those changes back into your Stitch app
    • Let other developers review and contribute code
  • Easily share custom services and functions between Stitch apps
  • Push code from development environments, to QA, and onto production.

stitch-cli currently includes 5 commands:

  • login: Log in using an Atlas API Key
  • logout: Deauthenticate as an administrator
  • whoami: Display Current User Info
  • export: Export a stitch application to a local directory
  • import: Import and deploy a stitch application from a local directory

While MongoDB Stitch lets you create complex applications, it also simplifies development by mapping your apps to standard JSON and JavaScript files and structures in a way that's easy to navigate.

An exported application is made up of a hierarchy of directories and files:

  • *.json: Files containing configuration data
  • *.js: Files containing JavaScript code

This is an example hierarchy for a Stitch IoT service that works with the backend MongoDB Atlas database and the REST APIs of 2 other cloud services – IFTTT and DarkSky.net:

impTemp
├── auth_providers
│   ├── anon-user.json
│   └── api-key.json
├── functions
│   ├── Imp_Write
│   │   ├── config.json
│   │   └── source.js
│   ├── TempTimeline
│   │   ├── config.json
│   │   └── source.js
│   └── controlHumidity
│       ├── config.json
│       └── source.js
├── services
│   ├── IFTTT
│   │   ├── config.json
│   │   └── rules
│   │       └── IFTTT.json
│   ├── darksky
│   │   ├── config.json
│   │   └── rules
│   │       └── darkSkyGET.json
│   └── mongodb-atlas
│       ├── config.json
│       └── rules
│           └── Imp.TempData.json
├── stitch.json
└── values
    ├── DarkSkyKey.json
    ├── DeviceLocation.json
    ├── MakerIFTTKey.json
    ├── maxHumidity.json
    └── minTemp.json

Installing stitch-cli

The simplest way is to install stitch-cli is as an NPM module:

npm install -g mongodb-stitch-cli 

Logging into the Atlas API

Before doing anything else, you need to create an Atlas API key & whitelist your local machine's IP Address, as shown here:

Creating an Atlas API key

You can now log into the admin API using stitch-cli, and confirm that you're logged in:

stitch-cli login --username=andrew.morgan --api-key=XXXXXXXX-XXX-4010-97a2-ad7c39d8cfb5

stitch-cli whoami
andrew.morgan [API Key: ********-****-****-****-ad7c39d8cfb5]

Export app, make local edits, and merge changes back into the same Stitch app

I noticed that I'm missing some error handling code in my functions. Rather than editing them all through the Stitch UI, I export the app so that I can work on the code in my favorite browser:

Export the Stitch app to the local file system:

stitch-cli export --app-id=imptemp-sobpa

I then add some extra code to handle cases where the MongoDB insert fails:

exports = function(data){

  //Get the current time
  var now = new Date();

  var darksky = context.services.get("darksky");
  var mongodb = context.services.get("mongodb-atlas");
  var TempData = mongodb.db("Imp").collection("TempData");

  // Fetch the current weather from darksky.net

  darksky.get({"url": "https://api.darksky.net/forecast/" + 
    context.values.get("DarkSkyKey") + '/' + 
    context.values.get("DeviceLocation") +
    "?exclude=minutely,hourly,daily,alerts,flags&units=auto"
  }).then(response => {
    var darkskyJSON = EJSON.parse(response.body.text()).currently;

    var status =
      {
        "Timestamp": now.getTime(),
        "Date": now,
        "Readings": data,
        "External": darkskyJSON,
      };
    status.Readings.light = (100*(data.light/65536));
    context.functions.execute("controlHumidity", data.temp, data.humid);
    TempData.insertOne(status).then(
      results => {
        console.log("Successfully wrote document to TempData");
      },
      error => {
        console.log("Error writing to TempData colletion: " + error);
      });
  });
};

I then apply the updates to the Stitch app (the CLI conveniently shows me the delta before I commit the merge):

cd impTemp
stitch-cli import --app-id=imptemp-sobpa --strategy=merge

--- functions/Imp_Write/source.js
+++ functions/Imp_Write/source.js
@@ -28,6 +28,9 @@
     TempData.insertOne(status).then(
       results => {
         console.log("Successfully wrote document to TempData");
+      },
+      error => {
+        console.log("Error writing to TempData collection: " + error);
       });
   });
 };

Please confirm the changes shown above: [y/n]: y
Successfully imported 'imptemp-sobpa'

You can then validate the changes through the UI:

Updated Stitch function, viewed through the UI

Summary

The Stitch CLI make it easier than ever to work with Stitch code – regardless of whether you’re an advanced user or just beginning. If you've created your first stitch app, try the Stitch CLI to start extending it – all from the comfort of your favorite editor. If not, try importing this example dashboard application.

Sign up for the Stitch free tier and see what you can create!

Stack Overflow & MongoDB Research Unveils Developer Productivity Struggles

Andrew Morgan

Company

As part of our mission to help developers build better apps faster by providing a database platform that doesn’t hold them back, we are always looking for new ways to better understand the challenges devs face.

We recently undertook some research with Stack Overflow, the largest, most trusted online community for developers, and the findings confirmed a lot of what we already knew and believed, such as developers’ critical role in driving innovation and growth in every type of organization. They also unveiled some troubling disconnects that still exist between business leaders and their developers.

The research, which surveyed more than 1,000 developers worldwide, revealed that despite developers being seen as key drivers of enterprise innovation – with 68% of respondents viewing developers as the primary drivers – companies are failing to capitalize on their skills and abilities. Many (33%) believe their companies aren't gaining a competitive advantage because they don’t understand the technical realities and opportunities that developers face.

What’s holding back developers?

Findings revealed three main challenges developers are dealing with that hold them back from fully delivering as innovators:

  1. Spending too much time in the wrong places: 41% of a developer’s working day goes towards the upkeep of infrastructure, instead of innovation or bringing new products to market. Al fifth of their time (20%) is spent in meetings or administration – yawn!
  2. Current job demands are heavy: 58% of developers work more than eight hours a day, 32% work weekends, and 23% fail to take all of their vacation days. The previous point plays a part in this, with developers working extra hours to catch up after wasting time on managing their infrastructure.
  3. Developer’s dilemma: Today’s application users flat out expect applications to integrate with other services to provide a richer, more complete experience. That could be the ability to make payments through Stripe or PayPal, or to log in faster with Facebook ID and send text messages from within an app via Twilio. But the monotonous, backend coding that is required to provide this improved user experience is taking the focus away from the frontend experience: 41% of a developers' time building a new application is spent connecting to backend services, rather than on developing features to makes their applications unique.
How developers spend their time each week

It was with these challenges in mind that we launched MongoDB Stitch. As developers ourselves, we recognize that web, mobile, and IoT apps today almost always include a combination of services like authentication systems, payment apps, messaging platforms, or internal microservices.

Stitch lets developers focus on building applications rather than on managing data manipulation code, service integration, or backend infrastructure. Whether you’re just starting up and want a fully managed backend as a service, or you’re part of an enterprise and want to expose existing MongoDB data to new applications, Stitch lets you focus on building the app users want, not on writing boilerplate backend logic.

56% of developers believe that developers are the main drivers of innovation within organizations today

The cloud: challenge or opportunity?

The survey also asked developers about their experiences with the cloud, resulting in some interesting insights:

  • Cloud wars: Nearly half of the developers surveyed (42%) say security regulations at their company are the primary barrier to furthering cloud adoption within the organization. Amazon Web Services (39%) is currently ahead of Microsoft Azure (19%) and Google Cloud Platform (13%) when it comes to where companies host their apps.
  • Reality check on cloud migration: In regards to migrating services to the cloud, nearly half of the developers surveyed (43%) said they currently used cloud services and had expansion plans. However, there are still considerable barriers to cloud adoption, with nearly half of those surveyed (42%) noting that security, privacy, or regulatory concerns were all hindrances to cloud migration. On top of this, less than half of today’s apps (45%) are developed primarily for the cloud.
  • Cost and productivity benefits of Cloud-hosted DBaaS are well understood: Well over half (60%) of the developers surveyed fully understand the cost benefits of cloud-hosted Database as a Service (DBaaS) in that you pay for what you use. Half (50%) noted that DBaaS results in increased developer productivity and a faster time to market.

MongoDB Atlas is MongoDB’s answer to these cloud concerns. Fully-managed with best-in-class security, Atlas is the easiest and most cost-effective way to get started building great applications on MongoDB. We take care of the operations and security best practices and let developers focus on driving innovation.

Give the devs what they want

The rise of the developer within organizations has been an ongoing trend for years, and business leaders are increasingly recognizing the value that developers provide in keeping their companies competitive.

But to truly maximize the innovation that developers can provide, software developers will need the right technology in place – technology that is intuitive and natural to work with instead of creating additional speed bumps in the development process. For developers to truly take their rightful place as Kingmakers of the enterprise, business leaders will need to continue to grow their understanding of how to best arm their developer teams to drive a competitive edge in a software-centric world.


About the author - Andrew Morgan

Andrew is part of the MongoDB product team, responsible for building the vision, positioning, and content for MongoDB’s products and services, including the analysis of market trends and customer requirements. Before joining MongoDB, Andrew was director of product management for MySQL at Oracle – with a particular focus on distributed, highly available databases. Prior to Oracle, Andrew worked in software development for telecoms with a focus on HA, in-memory, real-time databases.