Skip to content
the poor coder

create record mongoose model

Poor Man
Published
Reading time
2 min read
On this page

Create Record Mongoose Model#

Creating a record in Mongoose model is a simple process. First, we must define a schema that specifies the structure of our document. We can then create a model using this schema, and use it to save new documents to our database.

Defining a Schema#

The schema defines the structure of our document, including the types of data that can be stored and any validation rules. Here is an example of defining a schema for a simple user:

JavaScript

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  age: Number
});

const User = mongoose.model('User', userSchema);

In this example, we have defined a schema with three properties: name, email, and age. The name and email properties are required, and the email property is also unique. The age property is optional and can be a number.

Creating a Model#

Once we have defined our schema, we can create a model using the mongoose.model() method. This method takes two arguments: the name of the collection that our documents will be saved to, and the schema that defines the structure of our documents.

JavaScript

const User = mongoose.model('User', userSchema);

This creates a User model that we can use to save new documents to the users collection in our database.

Saving a Document#

To save a new document to our database, we can create an instance of our model and call the save() method:

JavaScript

const newUser = new User({
  name: 'John Doe',
  email: 'johndoe@example.com',
  age: 30
});

newUser.save((err, user) => {
  if (err) {
    console.error(err);
  } else {
    console.log(user);
  }
});

In this example, we have created a new user with a name, email, and age. We then call the save() method on our newUser instance, passing a callback function that will be called when the save operation is complete. The callback function takes two arguments: an error object, if an error occurred during the save operation, and the saved document.

We can also use the create() method on our model to create and save a new document in one step:

JavaScript

User.create({
  name: 'Jane Doe',
  email: 'janedoe@example.com',
  age: 25
}, (err, user) => {
  if (err) {
    console.error(err);
  } else {
    console.log(user);
  }
});

In this example, we have created a new user using the create() method on our User model. This method takes an object with the properties we want to set on the new document, and a callback function that will be called when the save operation is complete.

These are the two main ways to create and save new documents in Mongoose. There are many other methods available for querying, updating, and deleting documents in our database. Consult the Mongoose documentation for more information.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC