【问题标题】:Find nested embedded array with Mongoose/MongoDB使用 Mongoose/MongoDB 查找嵌套的嵌入式数组
【发布时间】:2022-09-29 00:10:31
【问题描述】:

我试图过滤掉文档中的嵌套数组元素,但没有显示任何内容。

这是我的架构:

//Product:
const productSchema = new mongoose.Schema(
  {
    productname: {
      type: String,
      required: [true, \'User must have a name\'],
      unique: true,
      validate: {
        validator: function (str) {
          return validator.isAlphanumeric(str, \'en-US\', { ignore: \' \' });
        },
        message: (props) => `${props.value} is not a valid username`,
      },
    },
    slug: String,
    price: {
      type: Number,
      required: [true, \'A product must have a price\'],
    },
    description: {
      type: String,
      trim: true,
    },
    images: [String],
    variants: [Variant], //Schema
  },
  {
    id: false,
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);


//Variant:
const variantSchema = new mongoose.Schema(
  {
    // product: {
    //   type: mongoose.Schema.ObjectId,
    //   ref: \'Product\',
    // },
    // size: {
    //   type: mongoose.Schema.ObjectId,
    //   ref: \'Size\',
    // },
    // color: {
    //   type: mongoose.Schema.ObjectId,
    //   ref: \'Color\',
    // },
    size: {
      type: String,
      enum: {
        values: [
          \'35\',
          \'35.5\',
          \'36\',
          \'36.5\',
          \'37\',
          \'37.5\',
          \'38\',
          \'38.5\',
          \'39\',
          \'39.5\',
          \'40\',
          \'41\',
          \'41.5\',
          \'42\',
          \'42.5\',
          \'43\',
          \'44\',
          \'44.5\',
          \'45\',
          \'46\',
          \'46.5\',
          \'47\',
          \'47.5\',
          \'48\',
          \'S\',
          \'M\',
          \'L\',
          \'XL\',
          \'XXL\',
          \'XS\',
          \'Onesize\',
        ],
        message: \'Please enter correct sizing format !\',
      },
      required: [true, \'Please enter sizing !\'],
    },
    color: { type: String, required: [true, \'Please enter color !\'] },
    quantity: Number,
  },
  {
    id: false,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

笔记: 变体被嵌入,因此被导出为模式。

所以我试图过滤掉图片中灰色的变体:

我所做的是通过 slug 找到产品并使用变体内部的颜色属性进行过滤。

const document = await Model.find({
 slug: req.params.slug,
 variants: {
  color: \'Grey\'
 }
});

它什么也没显示,0

我也试过\"variants.color\" : \"Grey\" 但这一次它给了我所有的结果。所以似乎过滤器没有应用或根本不起作用。

  • 您能否澄清“它给了我所有的结果。”短语中的“结果”是指产品还是变体?点表示法是正确的,它应该只返回具有至少 1 个匹配变体的产品。整体产品。如果您希望仅检索匹配的变体,则需要使用聚合框架来过滤变体数组。
  • 指变体。
  • @AlexBlex 我想返回具有匹配变体的产品,但 find 方法返回具有所有变体的产品。如何仅使用匹配的变体(以及产品)来做到这一点?

标签: node.js mongodb mongoose


【解决方案1】:

在这种情况下,您不需要虚拟机。根据populate() documentation,这可以通过“匹配”选项来实现。

解决方案应该是:

const document = await Model.find({
  slug: req.params.slug
}).populate("variants", {
  match: { color: "Grey" } // query for subdocument
});

【讨论】:

猜你喜欢
  • 2021-04-14
  • 2015-06-20
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多