Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.
Modal
Mongoose allow us to perform CURD operation using REST full Modals. A Modal is basically is an object/class which define all fields with validation and also connected to your MongoDB.
To perform operation ,create variables of the modal and call the save/ create method. Following is a sample Modal class
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name :{
type:String,
required: '{PATH} is required!'
},
bio: {
type:String
},
email:{
type:String
},
website:{
type:String
},
password:{type:String,required: '{PATH} is required!',default:"123"},
posts : [ {type: mongoose.Schema.Types.ObjectId,ref:'Post'} ]
},{
timestamps: true
})
module.exports = mongoose.model('User',UserSchema);
In the modal class you can store another document using ObjectID .Here is an example for the create document using the modal class
const user = await User.create({ name, email, bio, website })
That’s it. Following mongoose posts deserve your attention
- How to export multiple components in Reactjs - How to render export multiple components in React
- How to interact with input in Reactjs - How to handle state and event of input element in React apps
- How to populate multiple MongoDB objects on the same path in express-node - How to populate multiple objects in MongoDB in the same path
- How to populate selected fields in a mongo document path in expressjs - How to populate object in mongo doc at a specific path with specific fields
- How to populate and display nested mongo objectin express-node route - How to populate object in express-MongoDB app
- How to render mongo object in Reactjs - How to render MongoDB object/ObjectId in React/MERN app
- How to render list of mongo objects in React component - How to render list of mongo objects in Reactjs
- How to place custom route links in Reactjs component - How to place custom links to routes/page in Reactjs
- How to filter MongoDB object list in express-node app - How to filter a mongo object list in document in express-node app
- How to fix mongo object Type error in Reactjs - How to fix TypeError/undefined in React - MERN application