kRA*_*k3N 5 mongoose mongodb node.js
用户架构:
var UserSchema = new Schema({
name: { type: String, required:true },
email: { type: String, required:true, lowercase: true , index : { unique: true } },
password : { type: String, required:true , select:true },
blog_bookmarks: [{ type: String }]
});
Run Code Online (Sandbox Code Playgroud)
blog_bookmarks
用于为特定用户添加值的API
api.post('/add_bookmark_blog', function(req, res){
User.findOne({_id: req.query.user_id}, function(err, user){
if(err)
{
res.json(err)
}
else{
var blogid = req.body.blog_id;
user.find({ blog_bookmarks : blogid}, function(res1, result){
if(res1){
user.blog_bookmarks.push(blogid);
user.save(function(err) {
if(err){
res.json('ERROR at adding bookmark')
}
else {
res.json('bookmark for blog added')
}
})
}
else{
res.json('Already bookmarked')
}
});
}
})
});
Run Code Online (Sandbox Code Playgroud)
我想添加blog_id
到blog_bookmarks
数组只有它不存在,我不想要多个条目.
目前,user.find()
给出控制台错误
user.find()不是函数
怎么实现呢?
要避免重复值,请blog_bookmarks
使用$ addToSet运算符.
User.update({_id: req.query.user_id}, {$addToSet: {blog_bookmarks: blogid}})
Run Code Online (Sandbox Code Playgroud)
你user.find()
给了你一个错误,因为它应该User.find()
在开头有一个大写字母U.
归档时间: |
|
查看次数: |
1389 次 |
最近记录: |