create record mongoose model

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:


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.


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:


const newUser = new User({
  name: 'John Doe',
  email: '[email protected]',
  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:


User.create({
  name: 'Jane Doe',
  email: '[email protected]',
  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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe