Course

Creating Documents - Exercise

In this lesson, we'll practice creating documents in Mongoose in our NextJS application.

If you've been following along, you should already have a basic folder structure and a file for storing our actions. Your structure should look something like this:

app
lib
actions
user.actions.ts
models
user.ts
db.ts
Insight

If you've missed the setup and need to catch up, you can check out these lessons:

Setting Up



Creating Models

If you've already followed those lessons, you should know how this works.

In order to interact with our database, we need to follow a few crucial steps.

  1. Create a file in the actions folder hold our server actions. In this case, we already have a user.actions.ts file.

  2. Import the necessary models and dependencies. In this case, we'll need to import the User model and the connect function from db.ts.

  3. Create a function that will interact with the database. In this case, we'll create a function that will create multiple users.

  4. Export the function so we can use it in our application.

Our user.actions.ts file

If you followed along, our actions file should look something like this:

import User from "@/lib/models/user";

user.actions.ts;

export async function createUser(name: string, email: string) {
  await dbConnect();
  const user = await User.create({ name, email });
  return user;
}

Here, we have a function that creates a user in our database. We're using the User model and the dbConnect function to interact with our database.

We use to ensure that we're connected to our database before we create a user. This is important because we need to be connected to our database to interact with it.

We then use to create a new user in our database. We pass in the and as arguments to create a new user.

Finally, we return the user that was created.

Creating Multiple Users

Now that we have that refresher out of the way, let's work on a function to create based on what we learned in the previous lesson.

So first, create a function and name it . This function should take an array of users as an argument.

Give it a shot.

Hint
monkey
//
type UserArray = { name: string; email: string }[];
export async function createMultipleUsers(users: UserArray) {
  await dbConnect();
  // todo
}

We've created a function that takes an array of users as an argument. We're using the function to ensure we're connected to our database.

Now, using what you learned in the previous lesson, complete the function to create multiple users.

Hint
monkey
//
type UserArray = { name: string; email: string }[];
export async function createMultipleUsers(users: UserArray) {
  await dbConnect();
  const createdUsers = await User.create(users);
  return createdUsers;
}

Because the function can take an array of objects, we can pass in the array of users directly to create multiple users.

Now, we can create multiple users in our database by calling this function and passing in an array of users.

Calling this function in our application

Now that we have our function to create multiple users, let's call this function in our application.

Just like before, you can navigate to any server side component or page and import our function.

Can you create an array of users in the page and pass it into our function?

Hint
monkey
import { createMultipleUsers } from "@lib/actions/user.actions"
page.tsx;

export default async function Home() {
  const users = [
    { name: "Alice", email: "alice@email.com" },
    { name: "Bob", email: "bob@email.com" },
  ];

  await createMultipleUsers(users);
}

Conclusion

Can you test this out in your application? Try running your application and see if the users are created in your database.

Try creating the same function for your other models and see if you can create multiple documents in your database at once.

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

Find Documents in Mongoose - Basic Find Method