Teaching how to use MongoDB and expose it via NodeJS APIs

 

Hi, we are starting to use different technologies to build complex scenarios around APIs. It’s getting common to use popular NoSQL DBs like MongoDB.

For this reason, I decided to build this very easy to follow blog that will help you get started with MongoDB and build simple REST APIs using NodeJS via the Express module.

The use case is simple, we are going to build a MongoDB to store users information that eventually (in a future blog) we are going to use to send SMS and Voice call notifications… But for now we are keeping things simple, we are starting by demystifying the use of MongoDB with NodeJS.

The code that sends the SMS/Voice call notifications in NodeJS via Twilio is out of the scope of this blog, but if you want to use it, you can find it here.

Pre-requisites

 

This blog is about simplicity, so we are going to build a simple HelloWorld sample that starts from scratch and interacts (GET and POST) with a MongoDB via Express APIs.

 

Installing and playing with MongoDB

 

In this section, I am going to show you how to install and get your MongoDB up and running. This is going to be the DB that we are going to use in a future blog to send SMS and voice call notifications to people.

Note: I am using Ubuntu, adjust if using other OS (e.g. yum if using OEL/RH):

sudo apt-get install mongodb

  • Validate the installation by running mongo client – This should connect to the running MongoDB Server

  • In the mongo client prompt, create a simple test database called myTestDB:

    use myTestDB


  • The database doesn’t really exist until we add some data. The best thing of MongoDB is that you store JSON payloads into it. This makes it very friendly when interacting with APIs.

     

    In our case, we want to build a notification service and we want to store the contact details of the recipients. Something like:

     

    {“name”:”Carlos”, “mobile”:”+615550505″, “msg”:”Hello World”}

     

    Note that we are not defining ids, mongoDB will take care of it.

     

  • Still within the mongo client db type the following inserts each followed by ab enter:

     

    db.usercollection.insert({ “name” : “Carlos”, “mobile” : “+615550505”, “msg” : “+Hello World”})

    db.usercollection.insert({ “name” : “Dave”, “mobile” : “+616660606”, “msg” : “+Hello World again”})

    db.usercollection.insert({ “name” : “Serene”, “mobile” : “+617770707”, “msg” : “+Hello World once more”})

     

     

  • Now, simply retrieve the inserted collection

    db.usercollection.find().pretty()


  • You can also add a JSON array of JSON payloads, so that you can insert a bulk of entries to MongoDB. For example:

     

    users = [{“name”:”Callan”, “mobile”:”+618880808″, “msg”:”More stuff”},{“name”:”Alessia”, “mobile”:”+612220202″, “msg”:”Stuff plus stuff”}]

     

    db.usercollection.insert(users);

     

  • Once again, retrieve the inserted collection:

    db.usercollection.find().pretty()


  • You can also use the usercollection object to remove individual entries by _id:

    db.usercollection.remove( {“_id”: ObjectId(“ID_GOES_HERE”)});

  • To completely clean up all your entries, you can simply use:

    db.usercollection.remove()

     

  • If you only want to use mong client to switch to a MongoDB and print a specific collection you can do this:

     

    • From Linux shell:             mongo [db_name]
    • Then, from within the mongo client:

      c = db.[collection_name];

      c.find().pretty()

  • To list all Collections in current db:

    db.getCollectionNames()

  • If you are using a MongoDB that requires authentication, you might need to create a specific user(s) under specific databases. For this follow the next steps:

     

  1. Using mongo client, login to admin db as administrator:

     

    mongo admin –username root –password [GIVEN_PASSWORD]

     

    Note: If using Bitnami, [GIVEN_PASSWORD] is the same as the OS root password.

     

  2. Switch to your MongoDB database:

     

    use [DATABASE]

     

  3. Create a new user(s)

     

    db.createUser(

    {

    user: “USER-GOES-HERE”,

    pwd: “PASSWORD-GOES-HERE”,

    roles: [ “readWrite”, “dbAdmin” ]

    }

    )

     

    Note: Substitute USER-GOES-HERE and PASSWORD-GOES-HERE with your own values.

     

  4. That’s it, you can now use that username and password to authenticate and grant read and write access to your db. If using monk in NodeJS as shown below in this blog, make sure to add the credentials in the connection string. A simple way to achieve this is by adding “USER:PASSWD@” before the server name.

     

    For example:

     

    var db = monk(USER + ‘:’ + PASSWD + ‘@’ + MONGODB_SERVER + ‘:’ + MONGODB_PORT + ‘/’ + DB_NAME);

 

 

For more information: https://docs.mongodb.com/manual/reference/mongo-shell/

 

Ok, our MongoDB is behaving well. Let’s run it together with our Express NodeJS code.

 

Building a simple Express JS API that interacts with a MongoDB

 

Now that we have a running MongoDB server running, let’s build a simple NodeJS code that will expose simple Express REST APIs to interact with your MongoDB.

I am going to explain below those snippets that require explanation, however before continuing make sure that you have downloaded/cloned the NodeJS project found here: https://github.com/barackd222/s2v-iia-nodejs-mongodb-crud-demo

The bits that you need to pay special attention from the NodeJS code:

  • app.js – This is a very standard Express JS code that starts a service listening in a specific port. In terms of MongoDB, notice the following snippets:

     

    • You need to require mongodb module, as well as monk.

  • Monk is going to give you the ability to connect to a MongoDB running somewhere. Think of it as a JDBC connection, in the relational database world, if you like.

  • Configuring the Express app to add the Monk db as part of it, so that it can be accessed within the request variable in each REST API.

 

  • The rest is just adding the right headers for the requests and starting the listening service.

 

  • mongoDBCrudAPIs.js – This file is where I define my REST APIs. At the time of writing this blog I just added 2 APIs, one that GET all Users in the MongoDB and the other that POST a new user.

     

    • GET /users – This is a very
      typical Express GET API. Inside the body though that’s where we:
      • Retrieve the monk DB connection that points to the MongoDB
      • Builds the Collection object as we did in the MongoDB console before
      • Runs a Find ALL function on the collections object that it built in the previous step
      • Sends back the resulting JSON payload as the API response. Notice that you can iterate and do whatever you want with this result

  • POST /user – Once again, this is a very typical POST Express API that retrieves some JSON body elements from the request. In terms of MongoDB, the bits that are worthwhile showing are:
    • Retrieve the monk DB connection that points to the MongoDB

  • Builds the Collection object as we did in the MongoDB console before
  • Uses the collection to insert the new user in a JSON format
  • Then it just handles errors if any. Otherwise return a successful response


  • Package.json – Make sure to add the right modules, so that you can run your NodeJS Application without having to manually install dependencies. In this case I am using:
    • express – To build the REST APIs
    • mongodb – To bring internal MongoDB libraries
    • monk – To help establish the MongoDB db connection
    • body-parser – To parse the incoming JSON requests

 

Let’s test our NodeJS Application

If you have been following this blog, at this point you have a few users in your “myTestDB” MongoDB, that you enter via the mongo client command line earlier.

  • I am using Postman to call the APIs, you can use your preferred test REST client. Let’s first try to retrieve these users.

  • Great job, now invoke your POST API to create a new one. In my case this is the JSON that I am sending:

{“name”:”Jason”,

“mobile”:”+6115791010101″,

“msg”:”Hi. This message comes from POSTMAN “}

 

Note: Make sure to add your header Content-type set to application/json

 

 

  • Now call the GET
    /users API again and make sure your new user is retrieved

Congratulations, you just managed to do very basic, yet powerful tasks using MongoDB as the backstore and NodeJS to expose interactive REST APIs via Express JS

 

If you have any comment, feel free to contact any of the authors of solutionsanz.blog – We are here to help you!

Thanks,

 

 

 

 

 

 

 

 

 

 

 

How to build a Digital Information Management Process Application using the Oracle Cloud Platform.

Most organisations have to the deal with several kinds of digital artefacts that need publishing on various channels like Web, Mobile, TV, Social, Kiosks, Print, etc. And it is no brainer that organisations need a “Digital Artifacts”   repository that can help them to store these digital assets in a centralised repository. Also, it helps them to distribute very quickly on those channels as mentioned earlier to maintain brand consistency and ensure the rights asset displays on the right channel at the right time. However, most often the digital artefacts are produced in partnership with several teams internally and externally (Primarily design agencies). So the Digital Artifact repository is not only a storage container, but also needs to behave like a collaboration platform to engage with all the stakeholders internal and external to the same platform in real-time. To make it bit more complicated imagine the teams producing the digital assets (aka contributors) need to get all the necessary approvals before the artefacts are published or even consumed by different individuals. Given the nature of the organisations, business approvals can come in various shapes. For example, a Digital Artifact that needs to go out on TV for advertising needs a Product Team, a Branding Team, and Legal Team. The product team is responsible for the design of the digital artefact. The branding team ensures the assets produced are consistent with organisations marketing themes. The legal team task is to ensure the teams have done their due diligence and advise the product teams of the Risk level associated with the activities. So a single digital artefact has got its life-cycle, and there are various touch points before it lands on different social or mobile channels. Your Digital Artifact repository that needs to behave like a collaboration platform should also ensure it provides the capability for digital lifecycle management and manages approvals.  Fortunately, the Oracle Content and Experience, and Process Cloud platform addresses the exact above needs in a simple, configurable manner. I don’t need to mention the beauty of having such a platform in the cloud as it allows us to just focus on the problem we have at hand than worrying about installation, what kind O/S server/containers, technology skills, scalability, upgrades, etc.  You can read more about Content & Experience and Process Cloud platform that fulfils the requirements for Digital Artifact repository, collaboration, and workflow automation. Now that we have the context let’s focus on one of the problems at hand, how do we make the Digital Artifact go through an approval process using these platforms?

Continue reading “How to build a Digital Information Management Process Application using the Oracle Cloud Platform.”

Speed 2 Value: Innovation in Action Workshop

vijayozayr

At this Full day workshop, Vijay and Ozayr will show you:
Consumer-level collaboration and innovation are now expected in the workplace, using a combination of techniques and cloud solutions, supporting
– Fast Idea to Digital Model
– Integration & Process Tools
– Mobile & Multi-Channel Engagement & Analytics
– Internet of Things

REGISTER BELOW by clicking on the relevant location:

Melbourne – CLOSED Tuesday, 21 March 2017
Perth – CLOSED Tuesday, 28 March 2017
Sydney – CLOSED Tuesday, 4 April 2017
Auckland – CLOSED Tuesday, 11 April 2017
Wellington – CLOSED Thursday, 13 April 2017
Adelaide – CLOSED Friday, 19 May 2017
Canberra – CLOSED Tuesday, 23 May 2017
Brisbane – CLOSED Friday, 26 May 2017

Speed 2 Value: Developer Experience (DX) Workshop

jasonl

At this Full day session, Jason will show you how to:
Develop Apps at speed whilst maintaining quality, control and flexibility using latest Devops practices
– Drive innovative approaches to business outcomes
– Understand the “6 Key Things” for AppDev
– See the “6 Key Things” in action with Oracle Cloud
– Experience what APIs can do for business

REGISTER BELOW by clicking on the relevant location:

CLOSED Tuesday, 21 March 2017
CLOSED Thursday, 23 March 2017
Melbourne Postponed
Adelaide Friday, 12 May 2017
Sydney Postponed
Canberra Wednesday, 17 May 2017
Perth Postponed
Brisbane Wednesday, 24 May 2017

Speed 2 Value: Integration in Action Workshop

daver     carlos2

At this 9am to 3pm session, Dave & Carlos will show you SAAS and On-Premise Apps working together discussing:
– Integration Choices
– Why APIs are Cool
– Integration Patterns / Analytics
– What and Why MicroServices
– Machine Learning,
and Seeing chatbots, drones and robots participating

REGISTER BELOW by clicking on the relevant location:

Wellington CLOSED
Auckland CLOSED
Sydney CLOSED
Brisbane Wednesday, 12 April 2017 – CLOSED
Perth Thursday, 20 April 2017 – CLOSED
Melbourne Wednesday, 26 April 2017
Adelaide Thursday, 4 May 2017
Canberra Thursday, 22 June 2017

Speed 2 Value: Cloud in Action Workshop

franco

In this 2 Hour session,  Franco will explain how the Oracle Cloud Platform provides:

  • Many Infrastructure Entry Points
  • Applications Usage beyond original purpose
  • Mobile and Bot Enablement
  • Faster Building of New Apps

REGISTER BELOW by clicking on the relevant location:

Wellington CLOSED
Auckland CLOSED
Sydney CLOSED
Canberra CLOSED
Brisbane Thursday, 6 April 2017 – CLOSED
Perth Tuesday, 11 April 2017 – CLOSED
Melbourne Wednesday, 19 April 2017 – CLOSED
Adelaide Thursday, 27 April 2017 – CLOSED

Speed 2 Value Workshops: March – May 2017

Cloud in Action Seminar – Register Here

In this 2 Hour session,  you hear how Oracle Cloud Platform provides
– Many Infrastructure Entry Points
– Applications Usage beyond original purpose
– Mobile and Bot Enablement
– Faster Building of New Apps

Integration in Action Seminar – Register Here

At this 9am to 3pm session, see SAAS and On-Premise Apps working together discussing
– Integration Choices
– Why APIs are Cool
– Integration Patterns / Analytics
– What and Why MicroServices
– Machine Learning
and Seeing chatbots, drones and robots participating

Developer Experience Seminar – Register Here

At this 9am – 5pm session, Develop Apps at speed whilst maintaining quality, control and flexibility using latest Devops practices
– Drive innovative approaches to business outcomes
– Understand the “6 Key Things” for AppDev
– See the “6 Key Things” in action with Oracle Cloud
– Experience what APIs can do for business

Innovation in Action Seminar – Register Here

At this 9am – 5pm session, see how Consumer-level collaboration and innovation are now expected in the workplace, using a combination of techniques and cloud solutions, supporting
– Fast Idea to Digital Model
– Integration & Process Tools
– Mobile & Multi-Channel Engagement & Analytics
– Internet of Things