CRUD operations with Mongoose: Once you have defined your models using Mongoose, you can perform CRUD (Create, Read, Update, Delete) operations on your data. Here's an example of how to create a new user using Mongoose:
javascript
const User = require('./models/user');
const user = new User({
name: 'John',
age: 30,
email: 'john@example.com'
});
user.save().then(() => {
console.log('User created');
}).catch((error) => {
console.error(error);
});
In this example, we create a new user instance and call the save() method to save it to the database. We also handle any errors that may occur during the save operation.
No comments:
Post a Comment