在Mongoose模式中使用多个值的唯一文档
我有一个特殊情况,我们的collections需要根据电子邮件地址和sweepstakes_id的组合来确保每个文档都是唯一的。 我看遍了,但我无法find如何完成这种types的validation。
架构定义:
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 });
- 如何知道Mongoose的upsert是否创build了一个新文档?
- 在聚合过程中,mongoose / mongodb是否可以访问模式中的对象引用?
- NodeJS + MongoDB + Express – 查询不带任何结果
- 在MacOS上运行Windows上的meteor应用程序
- 如何replaceObjectId ref。 与来自MongoDB的实际对象(idealy在服务器端)?
- 提取mongoosew / Restify
- 用于数字列表的mongodb模式数据types(或不含特定大小的数字数组)
- MongoDB新闻源模式devise
- mongoose.connection.collections.collection.drop()每隔一次都会抛出错误