A RESTFUL API with MONGODB a Project Presented
Total Page:16
File Type:pdf, Size:1020Kb
A RESTFUL API WITH MONGODB A Project Presented to the faculty of the Department of Computer Science California State University, Sacramento Submitted in partial satisfaction of the requirements for the degree of MASTER OF SCIENCE in Computer Science by Satia Ahmed SPRING 2018 © 2018 Satia Ahmed ALL RIGHTS RESERVED ii A RESTFUL API WITH MONGODB A Project by Satia Ahmed Approved by: __________________________________, Committee Chair Dr. Ying Jin __________________________________, Second Reader Dr. Jun Dai ____________________________ Date iii Student: Satia Ahmed I certify that this student has met the requirements for format contained in the University format manual, and that this project is suitable for shelving in the Library and credit is to be awarded for the project. __________________________, Graduate Coordinator ___________________ Dr. Jinsong Ouyang Date Department of Computer Science iv Abstract of A RESTFUL API WITH MONGODB by Satia Ahmed MongoDB is a document based NoSQL database system which stores data in the form of JSON documents. The de-normalized structure of MongoDB document makes query performance efficient and cost effective. In this project, a daycare application is implemented using MongoDB as the database. To emulate real life data, Big Data documents are generated programmatically using data generator tool from Redgate Software Ltd, called SQL Data Generator, Visual Studio SSIS tool, and python scripting. To connect with the database and perform CRUD (Create, Read, Update, Delete) operations on the data using RESTful API, multiple methods were surveyed in this project. These studies include dependencies and methods to wrap MongoDB with Java, C# and nodeJS. Further, this project implemented RESTful API using nodeJS, wrapping MongoDB as the backend database. The frontend was rendered with AngularJS which communicates with the backend and allows users to query and operate on the data stored in MongoDB. A MVC (Model View Controller) v model was used to implement the REST API, where Model is our MongoDB database and AngularJS is our View. _______________________, Committee Chair Dr. Ying Jin _______________________ Date vi TABLE OF CONTENT Page List of Tables ................................................................................................................................... x List of Figures ................................................................................................................................. xi Chapter 1. INTRODUCTION ....................................................................................................................... 1 2. CORE CONCEPT OF MONGODB ............................................................................................ 3 2.1 Database Data Model............................................................................................................. 3 2.2 Always-On High Availability Feature ................................................................................... 4 2.3 Horizontal scaling .................................................................................................................. 4 2.4 Security and storage............................................................................................................... 5 2.5 Download and installation process ........................................................................................ 5 3. CREATING A BIG DATA DATABASE ................................................................................... 8 3.1 Designing the Document Structure ........................................................................................ 8 3.2 Creating the Database .......................................................................................................... 18 3.3 Generating JSON Documents .............................................................................................. 19 3.4 Populating the database ....................................................................................................... 19 3.5 Exporting and Importing Collection .................................................................................... 20 3.6 Changing Schema ................................................................................................................ 22 vii 4. COMMANDS AND QUERIES IN MONGODB ...................................................................... 23 4.1 CRUD Operations................................................................................................................ 24 4.1.1 Create ........................................................................................................................... 24 4.1.2 Read.............................................................................................................................. 29 4.1.3 Update .......................................................................................................................... 36 4.1.4 Delete ........................................................................................................................... 39 4.2 Query Documents ................................................................................................................ 41 4.2.1 Query Embedded Documents ....................................................................................... 41 4.2.2 Query Operator ............................................................................................................. 42 4.2.3 Projection of Array Elements ....................................................................................... 46 4.3 Aggregate Operation............................................................................................................ 48 4.3.1 Pipeline ......................................................................................................................... 48 4.3.2 $lookup for Left Outer Join .......................................................................................... 49 4.4 Queries used in the REST API ............................................................................................ 51 5. IMPLEMENT REST API USING MONGODB ....................................................................... 55 5.1 Introduction to REST........................................................................................................... 55 5.2 RESTful API With MongoDB as Backend ......................................................................... 57 5.2.1 MongoDB drivers ......................................................................................................... 57 5.2.2 Connecting with .Net: C# framework .......................................................................... 59 5.2.3 Connecting with Java framework ................................................................................. 61 5.2.4 Connecting with Node.js: JavaScript framework ......................................................... 65 viii 5.3 MEAN stack ........................................................................................................................ 67 5.3.1 Functional Requirements for the REST API ................................................................ 67 5.3.2 Setting up a MEAN project .......................................................................................... 69 6. CONCLUSION .......................................................................................................................... 82 References ...................................................................................................................................... 83 ix LIST OF TABLES Tables Page 2.5 MongoDB Version Supported by Different OS....................................................................6 5.2.1 MongoDB Drivers ............................................................................................................ 57 x LIST OF FIGURES Figure Page 3.1.a Collection daycareInfo for daycare branch information ..................................................... 9 3.1.b Collection for Employee information ............................................................................... 10 3.1.c 1:N Relationships between Program documents and Student documents ........................ 11 3.1.d N:N Relationship between Program documents and Employee documents ..................... 12 3.1.e Document Structure in Collection programInfo containing information about programs ........................................................................................................................... 13 3.1.f Array of Documents in Collection programInfo containing information about all Students in that program...................................................................................................14 3.1.g Embedded related documents in Array of Documents Collection programInfo ............... 15 3.1.h Embedded Documents in Array of Document Structure in Collection programInfo ....... 16 3.1.h Related Embedded Documents in Collection programInformation .................................. 17 3.5.a Importing documents to populate collection ..................................................................... 20 3.5.b Export Operation to download documents from the collection.........................................21 4.1.1.a Create operation by single document insertion ................................................................. 25 4.1.1.b Create Operation by multiple documents insertion