Node Guide

Menu
Menu

Endpoints with Mongoose

Adding a Create Endpoint

We can revisit the CRUDing methods from the first version of the application and refactor them to use Mongoose. Edit the createData function as follows:

SNIPPET: controllers/controllers.js
  1. The createData function receives a data from the router, that represents the data for the new document that will be created.

  2. Inside the function, the Film.create() method is used to create a new document in the MongoDB collection. The create() method is a convenient way to create a new instance of the Film model and save it to the database in a single operation. It takes the data parameter as an argument, which contains the properties and values for the new document.

  3. The await keyword is used to wait for the asynchronous operation of creating the new document to complete. This ensures that the function waits for the operation to finish before proceeding.

  4. If the document creation is successful, the newly created document is logged to the console using console.log(newDocument), and then it is returned as the result of the function.

  5. If an error occurs during the document creation process, the catch block is executed. The error is logged to the console using console.error('Error creating data:', error), and an object with an error message is returned indicating the failure to create the data.

Adding an Update Endpoint

For updates amend the updateData as follows:

SNIPPET: controllers/controllers.js
  1. The updateData function takes two parameters: id, which represents the ID of the document to update, and data, which contains the new data to update the document with.

  2. Inside the function, the Film.findByIdAndUpdate() method is used to find the document with the specified id and update it with the provided data. This method is specific to Mongoose and simplifies the update query by combining the find and update operations in a single step.

  3. The findByIdAndUpdate() method takes three parameters:

    • The id parameter is the ID of the document to update.
    • The data parameter contains the new data to update the document with.
    • The third parameter { new: true } specifies that the updated document should be returned as the result.
  4. The await keyword is used to wait for the asynchronous update operation to complete.

  5. If the update operation is successful, the updated document is returned as the result of the function.

  6. If an error occurs during the update process, the catch block is executed. The error is logged to the console using console.error('Error updating data:', error), and an object with an error message is returned indicating the failure to update the data.

The new Mongoose version uses the Film.findByIdAndUpdate() method, which removes the need to create an explicit ObjectID as we had to do previously.

Deleting with Mongoose

Finally amend the deleteData method to delete a document.

SNIPPET: controllers/controllers.js
  1. The deleteData function takes the id parameter, which represents the ID of the document to be deleted.

  2. Inside the function, the Film.findByIdAndDelete() method is used to find the document with the specified id and delete it from the MongoDB collection. This method is provided by Mongoose and simplifies the delete query by combining the find and delete operations in a single step.

  3. The findByIdAndDelete() method takes the id parameter and searches for a document with that ID in the collection. If found, it deletes the document.

  4. The await keyword is used to wait for the asynchronous delete operation to complete.

  5. If the delete operation is successful, the deleted document is returned as the result of the function.

  6. If an error occurs during the delete process, the catch block is executed. The error is logged to the console using console.error('Error deleting data:', error), and an object with an error message is returned indicating the failure to delete the data.

Again, the new Mongoose version removes the need to create an explicit ObjectID as Mongoose hands this for us.

Next: Typescript