1

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 .

1

1 Answer 1

1

I think this query will work for your collection and gives your desired result as per my understanding

db.collection.aggregate
([
{$match:{_id:PRODUCT_ID}},
{$project:{
    _id:'$_id',
    productDetails:{
        $filter: {
           input: "$productDetails",
           as: "productDetails",
           cond: { $eq: [ "$$productDetails._id", PRODUCT_ID ] }
        }
     },
    photoUrl :"$photoUrl",
    reqCarTypes : "$reqCarTypes",
    isDeleted : "$isDeleted",
    shop : "$shop",
    promotion : "$promotion",
    title : "$title",
    attributes : "$attributes",
    description : "$description",
    createdAt : "$createdAt",
    updatedAt :"$updatedAt",
}},
])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.