猫鼬唯一在对象的子项目中不起作用

时间:2019-02-18 17:41:22

标签: mongoose mongoose-schema

我尝试使我的模式起作用,但:(

Schema({
  name: String
  text: [
    {
      name: String,
      language: {
        type: String,
        required: true,
        unique: true
      }
    }
  ]
})

如果我希望他们不能以相同的语言添加2个文本,我该怎么办?

let texte = [
    { name: "first", language: "English" },
    { name: "duplicate", language: "English" }
  ];
  newData.text.addToSet(texte[0]);
  newData.text.addToSet(texte[1]);
  newData.text.addToSet(texte[1]);

如果我用相同的语言推送重复的文本,我不会出错

    {
  "_id": "5c6aff3e6c895637094427d7",
  "text": [
    {
      "_id": "5c6aff3e6c895637094427d8",
      "name": "first",
      "language": "English"
    },
    {
      "_id": "5c6aff3e6c895637094427d9",
      "name": "duplicate",
      "language": "English"
    },
    {
      "_id": "5c6aff3e6c895637094427da",
      "name": "duplicate",
      "language": "English"
    },
  ],
  "name": "test"
}

我希望有办法:(

0 个答案:

没有答案