![](/img/trans.png)
[英]How to make Mongoose unique key validation to pass if other key is different?
[英]how to generate unique key hash in combination two model attribute in mongoose
我試圖在兩個模型屬性的組合中生成hash
。
我有這樣的架構
const BlogPost = new Schema({
id: { type: String, required: true, unique: true },
empid: String,
date: Date
});
我想創建一個唯一的id
,它是empid and date
組合的hash
。如果生成相同的hash
,則gives
我錯誤。 我們可以結合empid and date
生成unique
嗎?
如果傳遞empid and date
將會生成相同的哈希值? 那給我錯誤
這是我的代碼https://codesandbox.io/s/lively-tree-hd0fo
try {
var blog = new BlogPostModel({
empid: "test123",
date: "19-Jul-2019"
});
console.log("before save");
let saveBlog = await blog.save(); //when fail its goes to catch
console.log(saveBlog); //when success it print.
console.log("saveBlog save");
} catch (error) {
console.log(error);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.