Tutorial
Data Inserting

Data Inserting

Manual Data Inserting

Setting up Mongoose

Before we can start inserting data into our MongoDB database, we need to set up Mongoose in our server-side code. We'll need to import the User and Post models and the data that we want to insert into the database.

Now we need to import User and Post from Model(Schema) and import users and posts from dataset.

server
├── index.js
import User from "./models/User.js";
import Post from "./models/Post.js";
import { users, posts } from "./data/index.js";

Adding Data to MongoDB

Once we have our models and data imported, we can add the data to our MongoDB database using the insertMany method. In the following code snippet, we're inserting the users data into the User collection and the posts data into the Post collection.

    User.insertMany(users);
    Post.insertMany(posts);
/* MONGOOSE SETUP */
const PORT = process.env.PORT || 6001;
mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
 
    /* ADD DATA ONE TIME */
    User.insertMany(users);
    Post.insertMany(posts);
  })
  .catch((error) => console.log(`${error} did not connect`));

Avoiding Duplicate Data

It's important to be careful when adding data to your MongoDB database, especially if you're doing it manually. If you accidentally insert the same data multiple times, you could end up with duplicate information that could cause issues later on.

To avoid this problem, we can comment out the insertMany calls after we've added the data once. This way, we'll only add the data to the database when we need to.

/* MONGOOSE SETUP */
const PORT = process.env.PORT || 6001;
mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
 
    /* ADD DATA ONE TIME */
    // User.insertMany(users);
    // Post.insertMany(posts);
  })
  .catch((error) => console.log(`${error} did not connect`));
Conclusion

Manually inserting data into a MongoDB database using Mongoose is a straightforward process. By following the steps outlined in this article, you can quickly and easily add data to your database, while also avoiding the pitfalls of inserting duplicate data.

Check Data in MongoDB

To check the inserted data in MongoDB when using the MongoDB Atlas cloud service, you can follow these steps:

  • Log in to your MongoDB Atlas account at https://cloud.mongodb.com/ (opens in a new tab).
  • Select the project that contains the database you want to check.
  • In the left-hand menu, click on Databases
  • You can see Cluster0 in the list of databases and connect , view monitoring, browse collections, and more.
  • Click on the browse collections button to see the collections in the database.
  • Now click on Collection tab to see the data in the collection.
  • In the right-hand side you can see refresh button to refresh the data in the collection.
  • In the left-hand side now you can see the database name and two collections name users and posts.
  • Now click on the posts collection to see the data in the collection.

By following these steps, you can easily check and manage the data in your MongoDB database when using the MongoDB Atlas cloud service.