gpt4 book ai didi

node.js - 在 Mongoose Schema 中使用多个值的唯一文档

转载 作者:IT老高 更新时间:2023-10-28 13:07:20 25 4
gpt4 key购买 nike

我有一个特殊情况,我们的集合需要根据电子邮件地址和抽奖事件 ID 的组合来确保每个文档都是唯一的。我已经看遍了,但我找不到如何完成这种类型的验证。

架构定义:

var submissionSchema = new Schema({
client_id: {
type: Schema.Types.ObjectId,
ref: 'Client',
index: true
},
sweepstakes_id: {
type: Schema.Types.ObjectId,
ref: 'Sweepstakes',
index: true
},
email: {
type: String,
index: true
},
data: {
type: Schema.Types.Mixed,
default: []
}
});

最佳答案

您可以强制使用包含两个字段的唯一索引:

submissionSchema.index({ email: 1, sweepstakes_id: 1 }, { unique: true });

关于node.js - 在 Mongoose Schema 中使用多个值的唯一文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14283503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com