راکت
حل شده

استفاده از Populate Virtual

1 پاسخ

سلام
وقتی که برای ارتباط بین Collection ها از Virtual استفاده میشه چطوری باید id رو توی virtual ذخیره کرد؟؟؟؟

const UserSchema = new Schema({
name: {
first: String,
family: String
},
username: String,
admin: {type: Boolean, default: false},
email: {type: String, required: true, unique: true},
password: {type: String, required: true},
age: Number,
}, {timestamps: true, toJSON: {virtuals: true}});

UserSchema.virtual('products', {
ref: 'Product',
localField: '_id',
foreignField: 'user_id',
});
const ProductSchema = new Schema({
    user_id: {type: Schema.Types.ObjectId, ref: 'User'},
    cat_id: {type: Schema.Types.ObjectId, ref: 'Category'},
    title: {type: String, required: true, unique:true},
    body: {type: String, required: true},
    price: {type: Number, required: true},
    imageUrl: {type: String, required: true},
}, {timestamps: true, toJSON: {virtuals: true}});

منظورم ذخیره شدن id محصولات در products هستش
ممنون میشم راهنمایی کنید من رو

ثبت پرسش جدید

پاسخ‌ها

1
محمد صادق کریمی
محمد صادق کریمی
@m.s.karimi.ubuntu

تخصص: برنامه نویس

بهترین پاسخ

ایرادم رفع شد نیاز به ذخیره کردن نداره....
خودش به صورت اتوماتیک میخونه id ها رو...

شاید پاسخ شما همین‌جا باشد

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام