문서가 다른 문서의 배열과 동일한 배열을 가질 수없는 데이터베이스 스키마를 만들고 싶습니다. 두 번째는 실패 할 수 있도록 어떻게이 유효성을 검사 할 수 있습니다, 지금은 같은 참가자들과이 대화를 만드는 경우몽구스 - 문서가 동일한 배열을 가질 수 없습니다.
var ConversationSchema = new Schema({
name: String,
participants: {
type: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
}
});
하지만, 셋째하지 않습니다 : 그래서, 스키마 conversations
이 있다고?
var conversation1 = new Conversation({
name: "Hello",
participants: ['12345', '09876']
});
var conversation2 = new Conversation({
name: "World",
participants: ['12345', '09876']
});
var conversation3 = new Conversation({
name: "Group chat",
participants: ['12345', '09876', '13579']
});
conversation1.save(); // Valid
conversation2.save(); // Invalid - conversation already exists
conversation3.save(); // Valid
이것은 스키마 유효성 검사가 아닌 비즈니스 로직처럼 보입니다. –