CNode

mongoose的一个小问题,请各位大大们指教

Iigooda发布于12 年前最后回复12 年前0 回复4095 浏览0 收藏

那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。

查看回复

回复 (0)

暂无回复,成为第一个参与讨论的人。
参与回复
登录后即可参与回复。登录