Answer Posted / Achyut Prakash
To use Mongoose with Node.js, first install it using npm (npm install mongoose). After installation, create a new file or include it in an existing one, import it at the top: const mongoose = require('mongoose'); To define a schema for your model, create a new object and specify its structure: const Schema = mongoose.Schema; const mySchema = new Schema({ /* your fields and their types here */ }); Then, create a model using the schema: const MyModel = mongoose.model('MyModel', mySchema); To connect to MongoDB database, use mongoose.connect(): mongoose.connect('mongodb://localhost/mydb', {useNewUrlParser: true});
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers