Introduction to Creating and Updating Data in MongoDB

Welcome, stargazers! Today, we're venturing into the realms of creating and updating data in MongoDB. It bears a resemblance to a space diary, where you log observations of new celestial objects and note changes in their properties over time.

How to Insert Documents into a Collection

Our data logbooks in MongoDB are referred to as documents. Have you discovered a new star? Let's create a log entry, or document, for it:

Here, we created a star object instance from the Star model using the new Star call and saved it into the database with the save() async method. Simple, isn't it?

At the end of the function, we called mongoose.connection.close();. This line closes the current connection to the MongoDB server. This helps in freeing up system resources that our application was using during the connection. It's a good practice to close unnecessary connections especially when the DB operations are no longer required or when all the tasks are finished. This maintains optimal application performance and prevents memory and connection leaks.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal