Предположим, что у нас есть две модели
1.Мобильный
2.Ноутбук
Мы хотим добавить функцию отзывов для клиентов в наше приложение. Для одной модели отзывов будет несколько ссылок.
Этот процесс немного сложный, но мы пройдем через это.
- Создайте модель отзыва
const mongoose = require('mongoose');
const reviewSchema = new mongoose.Schema(
{
review: {
type: String,
required: [true, 'Review is required'], //Review: Good product
min: 3,
max: 200,
},
rating: {
type: Number,
required: [true, 'Review is required'], //Rating: 5
min: 1,
max: 5,
default: 4,
},
product: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'onModel', // product => mobile,laptop
required: [true, 'Review must belong to a product'],
},
onModel: {
type: String,
required: true,
enum: ['Laptop', 'Mobile'],
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: [true, 'Review must belong to a user'], //user who reviewed your product
},
createdAt: {
type: Date,
default: Date.now(),
},
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true }, //for virtual property.
}
);
reviewSchema.pre(/^find/, function (next) {
this.populate({ path: 'product', select: 'name image slug' }); //this will populate the product details in the review route for all the find operations. example: api/review
this.populate({
path: 'user',
select: 'name image',
}); //this will populate the user details in the review route
next();
});
const Review = mongoose.model('Review', reviewSchema);
module.exports = Review;
- Создайте промежуточное программное обеспечение Review для запроса поста
exports.createReview=async (req,res,next)=>{
try{
const { review,rating,user,onModel,product} = req.body;
await Review.create({review,rating,user,onModel,product})
// logic
}catch(err){
//error logic
}
}
- Виртуальное заполнение в мобильной модели
const mobileSchema = new mongoose.Schema(
{
//logic
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
//virtual populating
mobileSchema.virtual('reviews', {
ref: 'Review', //referencing review model
localField: '_id', //mobile object id
foreignField: 'product', //Review model field for mobile object id
});
const Mobile = mongoose.model('Mobile', mobileSchema);
module.exports = Mobile;
- Популирование отзывов в мобильной модели
exports.getMobileById=async (req,res,next)=>{
try{
const mobile=await Mobile.findById(req.params.id).populate({ path: 'reviews', select: 'review rating user createdAt'});
//other logics
}catch(err){
//error logic
}
}
Спасибо за чтение
Связь со мной : Github LinkedIn
#mongoose #динамический #референс