Building Intelligent Apps with MongoDB and Google Cloud - Part 1

Jane Fine

#Technical#Cloud#Stitch#Developer

Data analytics is a perpetual underachiever. Every generation of tools promises us better insight and never quite delivers. So we get stuck re-platforming and re-designing, hoping the next iteration will finally get us to the intelligence utopia. Yet modern applications must provide rich experiences, offer decision support, and continuously learn and adapt to win their users. Analytics and AI are at the heart of these Intelligent Apps.

We decided to build an Intelligent App to demonstrate how easy it is to take advantage of ML and AI cloud services without hiring a team of data scientists. First, we built a simple e-commerce application - MongoDB SwagStore - using React and MongoDB Stitch with MongoDB Atlas on GCP. Stitch saved us hundreds of lines of code and our app was ready in days. But aside from implementing stock replenishment notifications with Stitch Triggers and Twilio, it wasn’t very intelligent... yet.

We enabled our SwagStore with a product recommendation engine. Rather than implementing a recommendation engine from scratch, we used Google Cloud ML to train and tune a TensorFlow model that implements a WALS collaborative filtering algorithm. We then used Google Cloud Endpoints to serve up these personalized recommendations.

When a user authenticates, MongoDB Stitch sends an HTTP GET request to the Google Cloud Endpoint to obtain a list of recommended products.

A Stitch Function updates the recommendations array in the user document with the returned result.

exports = function() {
  //services
  const gcp = context.services.get("GoogleCloudRec");
  const mongodb = context.services.get("mongodb-atlas");
  //my swagstore collection
  const users = mongodb.db("swagstore").collection("users");
  const products = mongodb.db("swagstore").collection("products");

  return users.findOne({user_id: context.user.id})
    .then(user => {
      if(!user.gcpId) {
        return [];
      }
      //URL to GCP cloud endpoint
      const url = `https://jfmlrecengine.appspot.com/recommendation?userId=${user.gcpId}`;
      return gcp.get({ url }).then(response => { 
        console.log("Retrieved Recommendations");
        return EJSON.parse(response.body.text());
      })
      .then(result => {
        // Get the product info for the array of product ids
        return products.find({id: {"$in": result.articles}}, {_id:0, id:1, name:1, image:1}).toArray();
      })
      .then(products => {
        console.log(JSON.stringify(products));
        // Write the products to the user document
        return users.updateOne({"gcpId": user.gcpId}, { $set: { "personalized_recs" : products}})
          .then(() => { return products });
      });
    });
};

So when Jane logs into SwagStore she will see these product recommendations:

And Jasper - different ones:

By using MongoDB Stitch combined with powerful cloud services and APIs you can build a recommendation system like this very quickly and plug it right into you operational app getting your developers and data scientists to work together, operationalize insight, and deliver intelligence to your customers. Give it a try!

Stay tuned for Part 2 where SwagStore becomes even more intelligent with an AI chatbot.