0

I have 3 collections in MongoDB with the following Schema:

 const userSchema = new Schema({
    firstname: {
        type: String,
        trim: true
    },
    surname: {
     type: String
    }
})
userSchema.virtual('products', {
    ref: 'product',
    localField: '_id',
    foreignField: 'user'
})

userSchema.virtual('carts', {
    ref: 'cart',
    localField: '_id',
    foreignField: 'user'
})
const User = mongoose.model('user', userSchema)

-----------

const Product = mongoose.model('product', {
    title: {
       type: String,
       required: true
    },
    description: {
        type: String,
        required: true
    },
    quantity: {
        type: Number,
        default: -1
    },

    user: {
       type: Schema.Types.ObjectId,
       ref: 'user',
       required: true
    }
 })

--------

const ItemSchema = new Schema({
    session_id: {
        type: String
    },
    quantity: {
        type: Number,
        required: true
    },
    price: {
        type: Number
    },
    total: {
        type: Number
    },
    product: {
        type: Schema.Types.ObjectId,
        ref: 'product',
        required: true
    }
})

const CartSchema = new Schema({
    items: [ItemSchema],
    user: {
        type: Schema.Types.ObjectId,
        ref: 'user',
        required: true
    },
    total_price: {
        type: Number,
        default: 0
    }
})
const Cart = mongoose.model('cart', CartSchema)

So let's say we have some data in these collections and I want to get the cart data of some user like this:

user = await User.findById(user_id).populate({
        path: 'carts',
        populate: {
            path: 'products',
            model: 'product'
        }
    })
return user.carts

I get the following response without giving me the product detail:

[
    {
        "total_price": 10,
        "_id": "5ff373073b92a40898c50508",
        "items": [
            {
                "_id": "5ff4d3b86404131811e392ef",
                "product": "5fe9ea426c3ff17b383dd599",
                "quantity": 5,
                "price": 2,
                "total": 10,
                "createdAt": "2021-01-05T21:01:44.269Z",
                "updatedAt": "2021-01-05T21:01:44.269Z"
            }
        ],
        "user": "5fe255a5543b7420c9c29c8b",
    }
 ]

How its possible to get the product detail also, and am I right with structure of the collections? Thanks

devMob
  • 60
  • 7

1 Answers1

0

Try this:

 const userSchema = new Schema({
    firstname: {
        type: String,
        trim: true
    },
    surname: {
     type: String
    },
    product: {
        type: Schema.Types.ObjectId,
        ref: 'product',
    },
    cart: {
        type: Schema.Types.ObjectId,
        ref: 'cart',
    })

For what you are trying to achieve, virtuals should not be necessary.

In Mongoose, a virtual is a property that is not stored in MongoDB. Virtuals are typically used for computed properties on documents.

Everything you need is stored in the DB and you are not computing anything.

Algo7
  • 2,122
  • 1
  • 8
  • 19