0

Whenever I want to insert a new document, I receive the following error: MongoError: cannot index parallel arrays [english] [german]

What's the proper way to work with two unique arrays in MongoDB? What are alternatives to my approach?

My scheme:

const generalWordENSchema = new mongoose.Schema({
    german: {
      type: Array, 
      required: true
    },
    english: {
      type: Array, 
      required: true
    },
    partOfSpeech: {
      default: null,
      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 })

Example data:

{
      "english": ["jacket"],
      "german": ["Jacke", "Jackett", "Sakko"],
      "partOfSpeech": "noun",
      "example": "My jacket is brown and has two pockets.",
      "definition": "",
      "image": "",
      "audio": "",
      "level": ""
},
Maurice
  • 141
  • 1
  • 9
  • Indexes on array fields are called as Multikey Indexes. A Compound Index (like you have defined with two array fields) is not allowed in a Multikey Index. – prasad_ Jul 23 '21 at 00:38
  • Damn. Does a workaround exist for it? Is there a better way to solve it than to add two more fields with the first word of each array? Thanks for your answer! – Maurice Jul 23 '21 at 06:28
  • Your index(es) should support the application's queries. Why do you need a compound index like that? – prasad_ Jul 23 '21 at 06:31
  • It's the database of my language learning app, so I want to make sure that every word exists only one time. Every word needs to be unique, so the progress per word is correct (progress is stored in another collection). – Maurice Jul 23 '21 at 07:02
  • (1) A unique index on an array field doesnt impose uniqueness within the same document . The uniqueness is only applied across documents in a collection. (2) You have the option to create two separate unique indexes - one for each field (if you require it). (3) When populating or adding to an array use the `$addToSet` array update operator - this will ensure uniqueness within an array within a document. – prasad_ Jul 23 '21 at 07:43
  • I don't want to check this. I need to check if the combination of English array, German array, and part of speech already exists in another document. The reason for this is that I store the users' progress/strength of each word in another collection. The frequency of the word repetition is decided by the progress/strength. If there's a duplicate document I couldn't guarantee that the word gets repeated correctly because one document would show strength 0, so the word gets repeated every day, while the others strength is 4 and gets repeated every 30 days. – Maurice Jul 23 '21 at 08:04
  • 1
    You can store english and german array field values within same array. The array can have sub-documents like `words`: [ { lang: "en", w: "jacket" }, "{ lang: "ge", w: "Jacke" }, ... ]. Now you can have unique compound index on the `words` + `partOfSpeech`. – prasad_ Jul 23 '21 at 08:48
  • Thanks a lot! Your solution would have looked more professional, but I solved it already otherwise in the meantime. The first element of each array is now working as the preferred answer to questions and also to make the unique index work. – Maurice Jul 23 '21 at 10:54

0 Answers0