【问题标题】:mongo db filter the result in array of objectsmongodb过滤对象数组中的结果
【发布时间】:2021-08-14 21:25:07
【问题描述】:

产品架构具有 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 }
)

例如我有一个这样的产品文档:

{
    "_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
}

然后我希望所有字段都返回,除了那些我没有设置 productDetails id 的字段。

我可以从前端获取 productDetails id,我希望返回如下内容:

{
    "_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
}

查看 productDetails 字段。 我使用 nodejs 和 typescript。

【问题讨论】:

标签: node.js typescript mongodb


【解决方案1】:

我认为此查询将适用于您的收藏,并根据我的理解给出您想要的结果

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",
}},
])

【讨论】:

    猜你喜欢
    • 2022-07-13
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2020-09-24
    相关资源
    最近更新 更多