C
C
cheeck81312018-10-22 23:09:39
JavaScript
cheeck8131, 2018-10-22 23:09:39

How to make Mongoose ignore empty values ​​in an array, with a unique parameter?

I have a schema

const schema = new mongoose.Schema({
    ...account,
    characters : [
        { ...character },
    ],
    stats,
});

const character = {
    name: { type: String, required: true, unique: true },
}

When creating a user with an empty character array, I get a mongoose error
admin.players index: characters.name_1 dup key: { : null }

How to make it so that unique is checked only for those arrays that are created?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cheeck8131, 2018-10-24
@cheeck8131

setting sparse: true solves the problem, but values ​​can be repeated in this array, except for arrays of other documents...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question