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:
The
createDatafunction receives adatafrom the router, that represents the data for the new document that will be created.Inside the function, the
Film.create()method is used to create a new document in the MongoDB collection. Thecreate()method is a convenient way to create a new instance of theFilmmodel and save it to the database in a single operation. It takes thedataparameter as an argument, which contains the properties and values for the new document.The
awaitkeyword 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.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.If an error occurs during the document creation process, the
catchblock is executed. The error is logged to the console usingconsole.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:
The
updateDatafunction takes two parameters:id, which represents the ID of the document to update, anddata, which contains the new data to update the document with.Inside the function, the
Film.findByIdAndUpdate()method is used to find the document with the specifiedidand update it with the provideddata. This method is specific to Mongoose and simplifies the update query by combining the find and update operations in a single step.The
findByIdAndUpdate()method takes three parameters:- The
idparameter is the ID of the document to update. - The
dataparameter 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.
- The
The
awaitkeyword is used to wait for the asynchronous update operation to complete.If the update operation is successful, the updated document is returned as the result of the function.
If an error occurs during the update process, the
catchblock is executed. The error is logged to the console usingconsole.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.
The
deleteDatafunction takes theidparameter, which represents the ID of the document to be deleted.Inside the function, the
Film.findByIdAndDelete()method is used to find the document with the specifiedidand 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.The
findByIdAndDelete()method takes theidparameter and searches for a document with that ID in the collection. If found, it deletes the document.The
awaitkeyword is used to wait for the asynchronous delete operation to complete.If the delete operation is successful, the deleted document is returned as the result of the function.
If an error occurs during the delete process, the
catchblock is executed. The error is logged to the console usingconsole.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