mongoose的一个小问题,请各位大大们指教
发布于 10 年前 作者 igooda 3355 次浏览 最后一次编辑是 8 年前

那node练手抓糗白,使用mongoose存入mongodb,想解决重复内容的问题

//solution_one: fail, duplicated item still be saved. why??
*****
content: { type: String, unique: true},//参考《mongoose for application development》
*****
//solution_two: successful
****
articleSchema.path('/content').validae(function(value,respond){
Article.find({content: value},function(err,data){
if(err) return respond(false);
if(data.length) {
return respond(false)
}else{
return respond(true)
}
})
},'duplicated')
*****

现在我想如果查到存在重复项,然后对重复项做update,有没有推荐的代码,比如说类似solution_two。

回到顶部