Course

Creating Documents in Mongoose

In this lesson, we will learn how to create documents in Mongoose.

Remember, a document is a record in a collection, and a collection is a group of documents.

So we might have a collection called , and each would be an individual user.

For a refresher on the terminology- check out The Terminology Guide

Creating a Document

To create a document, we call the method on the model.

Here is an example:

//
// Create a new user
//
const user = await User.create({
  name: "Alice",
  age: 30,
  email: "alice@email.com",
});

In this example, we are creating a new user with the name , age , and email .

How do we know which fields to include in the document?

monkey

The fields that we include in the document should match the schema that we defined for the model.

The method returns a the newly created document.

This is helpful because it allows us to access the document immediately after it is created.

//
const user = await User.create({
  name: "Alice",
  age: 30,
  email: "alice@email.com",
});

console.log(user);
//
const user = await User.create({
  name: "Alice",
  age: 30,
  email: "alice@email.com",
});

console.log(user);
Insight

Having access to the document immediately after it is created is useful because it allows us to perform additional operations on the document, or to use the _id of the document in other parts of our application.

Can you think of a scenario where you might want to access the document immediately after it is created?

monkey

There are many scenarios where you might want to access the document immediately after it is created.

A common example would be when creating a new "post" on a social media platform. After creating the post, you might want to redirect the user to the newly created post so they can see it- so we might use the _id of the post to generate a URL for the post.

Creating Multiple Documents

We can also create multiple documents at once by passing an array of objects to the method.

Here is an example:

//
// Create multiple users
//
const users = await User.create([
  { name: "Alice", age: 30, email: "alice@email.com"},
  { name: "Bob", age: 25, email: "bob@email.com"},
  { name: "Charlie", age: 35, email: "charlie@email.com"},
]);

In this example, we are creating three new users at once. You might use this approach when you need to create multiple documents in a single operation- such as when seeding a database with test data.

Insight

When creating multiple documents at once, the method will return an array of the newly created documents, instead of a single document.

Conclusion

In this lesson, we learned how to create documents in Mongoose.

We learned that we can create a single document by calling the method on the model, and passing an object with the fields for the document.

We also learned that we can create multiple documents at once by passing an array of objects to the method.

In the next lesson, we'll dive into how we can use these methods in a real application in NextJS.

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Creating Documents - Exercise