Learning Library

← Back to Library

Building a LoopBack MongoDB API

Key Points

  • Created a new LoopBack project named “customers” using the `apic loopback` CLI and added the MongoDB connector (`loopback-connector-mongodb`) to enable database communication.
  • Configured a MongoDB data source through the API Designer, supplying host and port details and testing the connection.
  • Defined a “Customer” model linked to the MongoDB data source, added required properties (first name, last name, zip code) with appropriate data types, and enabled automatic REST API generation.
  • Launched the application locally with the Microgateway, used the API Explorer to POST new customer records and GET the list of stored customers, verifying the end‑to‑end setup.
  • Demonstrated the basic LoopBack workflow for creating data sources, models, and RESTful APIs backed by MongoDB.

Full Transcript

# Building a LoopBack MongoDB API **Source:** [https://www.youtube.com/watch?v=YHqdJAiw6cc](https://www.youtube.com/watch?v=YHqdJAiw6cc) **Duration:** 00:03:07 ## Summary - Created a new LoopBack project named “customers” using the `apic loopback` CLI and added the MongoDB connector (`loopback-connector-mongodb`) to enable database communication. - Configured a MongoDB data source through the API Designer, supplying host and port details and testing the connection. - Defined a “Customer” model linked to the MongoDB data source, added required properties (first name, last name, zip code) with appropriate data types, and enabled automatic REST API generation. - Launched the application locally with the Microgateway, used the API Explorer to POST new customer records and GET the list of stored customers, verifying the end‑to‑end setup. - Demonstrated the basic LoopBack workflow for creating data sources, models, and RESTful APIs backed by MongoDB. ## Sections - [00:00:00](https://www.youtube.com/watch?v=YHqdJAiw6cc&t=0s) **Creating LoopBack API with MongoDB** - The speaker walks through setting up a LoopBack project, installing the MongoDB connector, configuring a datasource, and defining a Customer model with a generated REST API. ## Full Transcript
0:00in this video we'll create a new 0:02loopback project to record customer 0:04information to mongodb first run apic 0:07loopback to create an application and 0:09enter a name when prompted in this demo 0:12I'll name it customers select the 0:15default options but when asked about the 0:17kind of application choose an empty 0:22server change into the customer 0:24directory and run npm install loopb 0:28back- connector DM M OD db-- saave to 0:32install and save the connector which 0:35will allow our application to speak with 0:37mongodb after we set up our data source 0:40next run apic edit to open the API 0:43designer in your browser click log in 0:46with Blu miix then use your Blu miix 0:48credentials to log in now let's create a 0:51data source which will configure our 0:53connection to the database click the 0:55data sources tab then the plus button 0:58and enter a meaningful name name for the 1:00data source I'm naming mine mongodb in 1:03the next form fill in the necessary 1:06configuration information such as the 1:08host and Port of your database 1:11then click the diset in the upper right 1:13to save and test the connection to the 1:15data source we just defined let's go 1:18create a model now click models and then 1:21the plus button to add a new model I'll 1:23name this model customer since we'll use 1:25it to store customer 1:28information set the data source to the 1:31name of the data source created earlier 1:33in my case mongodb the public check boox 1:37indicates to our application that we 1:39would like it to create a rest API for 1:41this model click the plus button in the 1:43lower right to add a property to your 1:45model I'll add first name last name and 1:49zip code I want to be sure my 1:51application always includes these 1:53properties so I'll check the required 1:55box next to all of them also notice that 1:58I can select the type for for the 2:00property this will ensure zip code will 2:02always be a number and the names will 2:05always be strings click the diset in the 2:08upper right to save let's try out our 2:11new API click the play arrow in the 2:13lower left this starts a local instance 2:16of your application and micro Gateway so 2:18that you can try out your application 2:21locally now let's investigate our new 2:24API hit explore then we'll use the post 2:28customers API to create new customer 2:31entries click the generate Link in the 2:34parameters field to generate a random 2:36entry based on the model we made I can 2:39click call operation to save the model 2:42to the 2:43database I'll create one more entry and 2:46edit the ZIP code 2:51field I can then go to get customers API 2:55to get a list of all the customers in my 2:58database that concludes our tutorial of 3:01the basics of loopback data sources 3:03thanks for watching