With mongoose we can easily place list of objects and populate documents/objects in MongoDB.
The Modal objects
In the modal object of Post we can define a field for user which is another modal in our project.
The User Schema and ‘post’ list field
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name :{
type:String,
required: '{PATH} is required!'
},
posts : [
{type: mongoose.Schema.Types.ObjectId,ref:'Post'}
]
,{
timestamps: true
})
module.exports = mongoose.model('User',UserSchema);
The Post Schema
const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
title: {
type: String,
required: '{PATH} is required!'
},
content: {
type: String,
required: '{PATH} is required!'
},
}, {
timestamps: true
})
module.exports = mongoose.model('Post', PostSchema);
In the user we had defined a field posts (list ), which is intended to store the user posts object ( MongoDB object ID), which can be used for populating the post later.
In the express route method we have the postsByUser which fetch the user post in the user documents
postsByUser: async (req, res) => {
try {
const { id } = req.params;
console.log('User:'+id);
const user = await User.findById(id).populate('posts');
console.log('User ->' + user);
res.send(user.posts);
} catch(e) {
// statements
console.log(e);
res.sta(401).send({error:e})
}
}
The the route is called in the following manner
http://localhost:3005/user/find/post/5fe4ab1e69b8d525c44ec293
and the user posts will be shown as following
{
"posts": [
{
"comments": [],
"tags": [],
"_id": "5fedcb477563625708ca7429",
"title": "Welcome to react",
"content": "A hello world Reactjs app",
"user": "5fe4ab1e69b8d525c44ec293",
"createdAt": "2020-12-31T12:59:51.559Z",
"updatedAt": "2020-12-31T12:59:51.559Z",
"__v": 0
},
{
"comments": [],
"tags": [],
"_id": "5fedcbe67563625708ca742a",
"title": "Helo",
"content": "Hello word app",
"user": "5fe4ab1e69b8d525c44ec293",
"createdAt": "2020-12-31T13:02:30.724Z",
"updatedAt": "2020-12-31T13:02:30.724Z",
"__v": 0
},
}
That’s it. Following mongoose posts deserve your attention
- 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
- How to create document using mongoose in express-node app - How to save MongoDB document to the collection using mongoose REST operations
- How to create Modal for CURD using Mongoose in express-node app - How to create a mongoose Modal class for performing CURD operation
- How to find document byId using mongoose in express-node app - How to find MongoDB document using MongoDB ObjectId using mongoose driver
- How to place list of objects in MongoDB-Express app - How to add list of sub document in MongoDB-Express app using mongoose
- Use Object id to set relationship in MongoDB-Express app - How to add sub document in MongoDB using Express-mongoose
Like this:
Like Loading...