0

我们需要识别(并避免)重复记录,这些记录包含在父文档的数组中,即:

{
  _id: 1,
  item: "abc",
  stock: [
    { size: "S", color: "red", quantity: 25 },
    { size: "S", color: "blue", quantity: 10 },
    { size: "M", color: "blue", quantity: 50 }
  ]
}
{
  _id: 2,
  item: "def",
  stock: [
    { size: "S", color: "blue", quantity: 25 },
    { size: "M", color: "blue", quantity: 5 },
    { size: "M", color: "black", quantity: 10 },
    { size: "L", color: "red", quantity: 2 }
  ]
}
{
  _id: 3,
  item: "ijk",
  stock: [
    { size: "S", color: "red", quantity: 25 },
    { size: "S", color: "blue", quantity: 10 },
    { size: "M", color: "blue", quantity: 50 }
  ]
}

在此示例中,具有 _id: 1 和 _id:3 的项目是重复的,因为数组的所有元素都完全相同

我们一直在尝试使用以下参考,但它并没有考虑到整个数组的所有元素,而只是考虑了一个属性,例如color

https://docs.mongodb.com/manual/core/index-multikey/#multikey-embedded-documents

如果您有任何其他可以帮助我们的建议或解决方法,我们将不胜感激:)

4

0 回答 0