0

I try to insert multiple documents into my MongoDB collection, but whatever I do, I get a duplicate error. I made sure that there should be no duplicates possible by dropping the whole collection.

I tried it with .insertMany(), .save(), .create() - none of them do work. Though the docs get inserted, I still get the duplicate error 11000.

My function to insert the docs:

Words.prototype.addManyGeneralWordsEN = async function(words) {
    await generalWordEN.create(words).then((res) => {
        console.log(res)
    })
    .catch((err) => {
        console.log(err.code)
    })
}

// add words to database
await this.addManyGeneralWordsEN(wordsToAdd)

My schema:

const generalWordENSchema = new mongoose.Schema(
  {
    german: {
      type: String, 
      required: true
    },
    english: {
      type: String, 
      required: true
    },
    partOfSpeech: {
      required: true,
      type: String
    },
    example: {
      default: null,
      type: String, 
    },
    defintion: {
      default: null,
      type: String, 
    },
    image: {
      default: null,
      type: String, 
    },
    audio: {
      default: null,
      type: String, 
    },
    level: {
      default: null,
      type: Number, 
    },
  }
)

generalWordENSchema.index({ german: 1, english: 1, partOfSpeech: 1}, { unique: true })

module.exports = generalWordENSchema

My sample data:

[
  {
    "english": "clothes",
    "german": "Kleidung",
    "partOfSpeech": "noun",
    "example": "My wife's wardrobe is filled with beautiful clothes.",
    "definition": "",
    "image": "",
    "audio": "",
    "level": ""
  },
  {
    "english": "men's clothing",
    "german": "Herrenbekleidung",
    "partOfSpeech": "noun",
    "example": "Men's clothing is on the second floor.",
    "definition": "",
    "image": "",
    "audio": "",
    "level": ""
  }
]
Maurice
  • 141
  • 1
  • 9

1 Answers1

1

The problem is probably on this line

generalWordENSchema.index({ german: 1, english: 1, partOfSpeech: 1}, { unique: true })

You created an index for the collection and used partOfSpeech as unique, but you have two documents with the same value noun.

It should work if you change it to:

generalWordENSchema.index({ german: 1, english: 1 }, { unique: true });

You also have a typo on the Schema declaration that might cause you different issues. You typed defintion instead of definition.

Rafael Freitas
  • 201
  • 2
  • 7
  • Thanks for your answer! How do I create an index where the combination of those three values is unique and not just one field? – Maurice Jul 24 '21 at 12:21
  • Why should `generalWordENSchema.index({ german: 1, english: 1, partOfSpeech: 1}, { unique: true })` be wrong? That's the correct syntax for combinations of multiple values for an unique index according to the MongoDB docs. I don't get it why it throws me that error. The collection is completely new and there are no duplicate objects in my JSON. What's also weird is that all objects still get stored in the database, even though it throws this error. – Maurice Jul 24 '21 at 17:01
  • Does it throw an error when you remove `partOfSpeech`? I checked the docs and it should be fine to use the three of them for a compound index. – Rafael Freitas Jul 24 '21 at 17:08
  • I think we need more info provided, then. Perhaps prints of the errors? – Rafael Freitas Jul 24 '21 at 18:41