How do I use a Mongoose model for CRUD?
User.create(data) to create, User.find(query) to read, User.findByIdAndUpdate(id, update) to update, User.findByIdAndDelete(id) to delete. The model provides all the methods you need to interact with documents.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Mongoose Schemas and Models Explained for Node.js
A schema defines the structure, types, and validation of documents. A model is compiled from a schema and provides CRUD methods to interact with documents. The schema is the blueprint; the model is the tool.
Create a new schema with new mongoose.Schema({ field: type }). For example, const userSchema = new mongoose.Schema({ name: { type: String, required: true }, email: { type: String, unique: true }, age: Number }).
Use mongoose.model('ModelName', schema). For example, const User = mongoose.model('User', userSchema). The model name maps to a collection (User becomes 'users'), and you use the model for all CRUD operations.
Still have questions?
Browse all our FAQs or reach out to our support team
