ok.
I try it. got the error again.
here is my fixed code, as you suggested I added the decodedUserId to my wordSchema and to the WordSchema.index:
import mongoose, { Schema, model } from 'mongoose';
import { UserModel } from '../users/userModel';
export class Word {
// index: number;
en_word: string;
he_word: string;
id: string;
constructor({en_word, he_word, decodedUserId }: {en_word: string, he_word: string, decodedUserId: string}){
// this.index = index;
this.en_word = en_word;
this.he_word = he_word;
this.id = decodedUserId;
}
}
//deine a schema
export const WordSchema = new Schema({
en_word: {
type: String,
required: true,
//unique: true
},
he_word: {
type: String,
required: true,
//unique: true
},
decodedUserId: {
type: String,
required: true,
}
});
// Create a unique compound index for en_word, he_word
WordSchema.index({ en_word: 1, he_word: 1, decodedUserId: 1}, { unique: true });
export const WordModel = model("word", WordSchema)
export const words: Word[] = [];
const UserWordsSchema = new mongoose.Schema({
wordsId: {type: Schema.Types.ObjectId, ref: WordModel},
userId: {type: Schema.Types.ObjectId, ref: UserModel},
});
// Create a unique compound index for UserWordsSchema
UserWordsSchema.index({ wordsId: 1, userId: 1}, { unique: true });
export const UserWordsModel = mongoose.model("userwords", UserWordsSchema);
I succeeded in adding the first word to the DB but when I tried to add the second word to the same user I got the same error:
MongoServerError: E11000 duplicate key error collection: translationGame.words index: index_1 dup key: { index: null }
at D:\GitRepo\translate_game_with_DB\translate-game-with-DB\node_modules\mongoose\node_modules\mongodb\src\operations\insert.ts:85:25
at D:\GitRepo\translate_game_with_DB\translate-game-with-DB\node_modules\mongoose\node_modules\mongodb\src\operations\command.ts:173:14
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
index: 0,
code: 11000,
keyPattern: { index: 1 },
keyValue: { index: null },
[Symbol(errorLabels)]: Set(0) {}
}
how can I see (in my consol.log for example) the keyValue of the index (the one I get null all the time)? there is a way to get it?
my code is as shown in the documentation of the process, what am I doing wrong?