Hope you are doing good.
I have a schema like this:
const Person = new mongoose.Schema({
username: {
type: String,
required: [true, "Name is a required field"],
unique: [true, "Another Person with the same name already exists"],
trim: true
},
friends: [
{
name: {
type: String,
required: [true, "Every friend must have a name"],
unique: [true, "Another friend with the same name already exists"],
trim: true
},
favoriteFood: String
}
],
createdAt: {
type: Date,
default: Date.now()
}
});
here I want name just be unique inside friends array, obviously different persons can have the same friend. but MongoDB does not let me define two person with the same name inside different persons object with this implementation. how can I do that?how to force friends name only be unique for one person?
another thing I found is if I try to add two friends with the same name under one person it will be accepted, but if you try to add same friend name across two different persons, it will throw the error of duplication. it is the exact opposite of what it should be, or at least opposite of what I want.
thanks.