Mongo Bears
Topics:
- Databases
- MongoDB
- ODMs
- Mongoose
- Performing CRUD operations.
Description
We will use Node.js and Express to write an API that can perform CRUD (Create, Read, Update and Destroy) operations on Bears stored in a MongoDB Database.
Software Requirements
For this project you need to have MongoDB Community Edition installed and running. Having a local instance of MongoDB running on your system is the preferred option.
Alternatively, you can sign up for an account from a Database As A Service (DBAAS) provider like MongoDB Atlas or mlab. Both DBAAS providers offer a free tier with 500MB size limit that can be used for development and testing.
Using a Local MongoDB Server
If you don't have MongoDB installed, please click on this link for instructions on how to install and run the Community Server and the mongo shell. Follow the instructions for your Operating System.
After MongoDB is installed, follow the instructions on the documentation to start the server. Then run the mongo shell from a separate terminal and execute the show dbs
command. If all goes well you should see a list of available databases, similar to the sample below.
> show dbs
admin 0.000GB
local 0.000GB
Getting the Starter Files
- Fork and Clone this repository.
- CD into the folder where you cloned the repository.
- Type
yarn
ornpm install
to download all dependencies listed insidepackage.json
. - After all dependencies finish downloading without errors, type
yarn start
ornpm start
to start the server.
Use Postman to Test the API.
- Make a GET Request to http://localhost:5000 using Postman. The response should be the following JSON object:
{
"api": "running"
}
Connect API Server to MongoDB
Use yarn or npm to add mongoose to to the project.
Inside server.js
, require mongoose and use it to connect your API to the beardb
database in your MongoDB Server (local or remote). If the beardb
database does not exist, it will be created automatically by MongoDB.
When the connection to MongoDB succeeds, log the following message to the console: "Successfully Connected to MongoDB".
If there is an error connecting to the database, log the following message to the console: "Database connection failed".
Create Bear Schema
In a separate file, create a Schema for the bears
collection. Each Bear document should conform to the following object structure:
{
species: "American Black Bear", // String, required
latinName: "Ursus americanus", // String, required
createdOn: Mon Aug 14 2017 12:50:16 GMT-0700 (PDT) // Date, required, defaults to current date
}
Generate Bear Model
Use mongoose to generate a Bear
model that can be used to perform operations on bear documents. Remember to export the model to make it available for importing into other parts of the application.
Create API Endpoints
Configure the API to respond to the following routes:
Method | Endpoint | Description |
---|---|---|
POST | /api/bears | Creates a bear using the information sent inside the request body . |
GET | /api/bears | Returns an array of all the bear objects contained in the database. |
GET | /api/bears/:id | Returns the bear object with the specified id. |
DELETE | /api/bears/:id | Removes the bear with the specified id and returns the deleted bear. |
PUT | /api/bears/:id | Updates the bear with the specified id using data from the request body . Returns the modified document, NOT the original. |
Endpoint Specifications
When the client makes a POST
request to /api/bears
:
-
If the request body is missing the
species
orlatinName
property:- cancel the request.
- respond with HTTP status code
400
(Bad Request). - return the following JSON response:
{ errorMessage: "Please provide both species and latinName for the bear." }
.
-
If the information about the Bear is valid:
- save the new Bear the the database.
- return HTTP status code
201
(Created). - return the newly created Bear Document.
-
If there's an error while saving the Bear:
- cancel the request.
- respond with HTTP status code
500
(Server Error). - return the following JSON object:
{ errorMessage "There was an error while saving the bear to the database" }
.
When the client makes a GET
request to /api/bears
:
- If there's an error in retrieving the Bears from the database:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ errorMessage "The bear information could not be retrieved." }
.
When the client makes a GET
request to /api/bears/:id
:
-
If the Bear with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The bear with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error in retrieving the Bear from the database:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ errorMessage "The bear information could not be retrieved." }
.
When the client makes a DELETE
request to /api/bears/:id
:
-
If the Bear with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ errorMessage: "The bear with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error in removing the Bear from the database:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ errorMessage "The bear could not be removed" }
.
When the client makes a PUT
request to /api/bears/:id
:
-
If the Bear with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The bear with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error when updating the Bear:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ errorMessage "The bear information could not be modified." }
.
Additional Notes
Stop the MongoDB database server when not in use to save computer resources.