product schema has an array field of productDetails .
const ProductSchema = new Schema(
{
title: [{ lang: { type: String, upperCase: true }, value: String, _id: false }],
attributes: [
{
attributeGroup: { type: mongoose.Types.ObjectId, ref: 'AttributeGroup' },
att: [{ type: mongoose.Types.ObjectId, ref: 'Attribute' }]
}
],
shop: {
type: mongoose.Types.ObjectId,
ref: 'Shop'
},
promotion: {
percent: Number,
discountFrom: Date,
discountTo: Date
},
preparationTime: Number,
description: [{ lang: { type: String, upperCase: true }, value: String, _id: false }],
photoUrl: [String],
productDetails: [
{
size: {
enum: ['SMALL', 'MEDIUM', 'LARGE'],
type: String
},
price: Number,
stock: { type: Number, min: 0, default: 0 },
afterDiscountPrice: Number
}
],
reqCarTypes: [
{
type: mongoose.Types.ObjectId,
ref: 'ReqCarType'
}
],
isDeleted: { type: Boolean, default: false }
},
{ timestamps: true }
)
for example I have a product document like this:
{
"_id" : ObjectId("60aa201525d6cd24d873d5dc"),
"photoUrl" : [
"http://lorempixel.com/640/480",
"http://lorempixel.com/640/480"
],
"reqCarTypes" : [],
"isDeleted" : false,
"shop" : null,
"promotion" : {
"percent" : 36,
"discountFrom" : ISODate("2021-01-23T09:27:49.623Z"),
"discountTo" : ISODate("2021-12-25T09:27:49.624Z")
},
"title" : [],
"attributes" : [],
"description" : [],
"productDetails" : [
{
"price" : 40,
"afterDiscountPrice" : 12,
"_id" : ObjectId("60aa201525d6cd24d873d5dc"),
"size" : "SMALL"
},
{
"price" : 50,
"afterDiscountPrice" : 12,
"_id" : ObjectId("61aa201525d6cd24d873d5dc"),
"size" : "MEDIUM"
}
],
"createdAt" : ISODate("2021-05-23T09:27:49.641Z"),
"updatedAt" : ISODate("2021-05-23T09:27:49.641Z"),
"__v" : 0
}
then I want all the fields to return except those that I haven't set the productDetails id.
I can get productDetails id from frontend and I expect to returns something like this:
{
"_id" : ObjectId("60aa201525d6cd24d873d5dc"),
"photoUrl" : [
"http://lorempixel.com/640/480",
"http://lorempixel.com/640/480"
],
"reqCarTypes" : [],
"isDeleted" : false,
"shop" : null,
"promotion" : {
"percent" : 36,
"discountFrom" : ISODate("2021-01-23T09:27:49.623Z"),
"discountTo" : ISODate("2021-12-25T09:27:49.624Z")
},
"title" : [],
"attributes" : [],
"description" : [],
"productDetails" : [
{
"price" : 40,
"afterDiscountPrice" : 12,
"_id" : ObjectId("60aa201525d6cd24d873d5dc"),
"size" : "SMALL"
}
],
"createdAt" : ISODate("2021-05-23T09:27:49.641Z"),
"updatedAt" : ISODate("2021-05-23T09:27:49.641Z"),
"__v" : 0
}
check out productDetails fields. I use nodejs and typescript .