Document management using MongoDB (Part 1: Uploading document)
- Nodejs should be installed on your machine. To install Node.js please visit (https://nodejs.org)
- MongoDB should be installed on your machine. To install MongoDB please visit (http://www.mongodb.org/downloads)
MongoDB is a free form database which has ability to store structured and unstructured data. In our example below we will see how to upload a file via SAP UI5 application and store it in MongoDB.
We are going to use Node.JS as middleware which has already given sample of APIs to interact with MongoDB, I will cover it in my next blogs.
Let the fun begin!!
Step 1: Develop SAP UI5 screen to upload document.
I am using Eclipse IDE for writing SAP UI5 code. Initially we created SAP UI5 view.xml (refer image 1.1 view.xml)
Once our view is ready it’s time to develop controller.js (refer image 1.2 controller.js )
Step 2: Setup MongoDB
Here in step 2, install MongoDB (suppose installed in D drive) in your system, go to ‘bin’ folder and start the server using ‘Command Prompt’ as shown below
Path : D:\MongoDB\Server\3.2\bin>mongod
2.1 Starting MongoDB Server
“mongod” is the command for starting the server. Once server gets started it will automatically gets connected to port no. 27017
Step 3: Logic in NodeJs to upload document
Now we will write code in NodeJs for uploading document in MongoDB.
3.1 Nodejs logic
As shown in above fig. 3.1, we have provided uri=’mongo://localhost/files’ where ‘files’ is Database present in MongoDB. (Note: No need to create database manually, mongoDB will create it automatically).
Step 4: Connecting pieces
We are done.. thats it.
Here we will see the flow of code, initially view.xml gets called i.e. when we run our code we get following output and we will select any random file (suppose here with name “SAP UI.pdf”)
Note: Dont forget to start nodejs file.
4.1 Selecting a document
Now When we click on Upload button, “getData” function will get called which is written in controller and then Nodejs function will get called i.e. “/uploadFiles”.
And finally, file will get uploaded in MongoDB. We have a MongoDB management tool known as “Robomongo” where we can see document details which we have uploaded else we can also see it in command prompt.
4.2 Screenshot of file saved in Robomongo