列出所有产品并检查它们是否已经在您最喜欢的 MongoDB 中

问题描述

我在 MongoDB 中有以下 3 个架构

1. const FavoriteSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,ref: 'user',required: true
    },product: {
        type: Schema.Types.ObjectId,ref: 'product',required: true
    }
},{ timestamps: true })


2. const ProductSchema = new Schema({
    title: {
        type: String,description: {
        type: String,quantity: {
        type: Number,default: -1
    }
    user: {
        type: Schema.Types.ObjectId,required: true
    }
})

ProductSchema.virtual('favorites',{
    ref: 'favorite',localField: '_id',foreignField: 'product'
})


3. const UserSchema = new Schema({
    firstname: {
        type: String,trim: true
    },surname: {
        type: String
    }
})

UserSchema.virtual('products',{
    ref: 'product',foreignField: 'user_id'
})

UserSchema.virtual('favorites',foreignField: 'user'
})

假设有很多产品,我想显示产品列表以及产品是否在用户最喜欢的列表中。

所以我喜欢:

ProductCollection.find().populate('user').populate('favorites')

我只得到产品列表和产品的用户信息,而不是最喜欢的。 不知道为什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...