【发布时间】:2017-08-18 02:03:54
【问题描述】:
我想知道如何使用 mongoose 更新喜欢的数组:
var postSchema = new mongoose.Schema({
author: String,
content: String,
date: String,
likes: [{theID: String}],
numDate: Number
});
var UserSchema = mongoose.Schema({
username: {
type: String
},
password: {
type: String
},
email: {
type: String
},
first: {
type: String
},
posts: [postSchema],
last: {
type: String
},
followers: [{theID: String}],
following: [{theID: String}],
img: { data: Buffer, contentType: String },
admin: {type: Boolean, default: false}
});
我可以通过以下方式将新帖子等内容推送给数据库中的某个用户 这样做:
User.update({_id: req.user.id}, {
$push: {"posts": {_id : req.body.theData}}
}, function(err, user){
res.redirect('profile');
});
是否有类似的方法可以查看特定用户,然后查看用户拥有的特定帖子并更新它以将字符串推送到 likes 数组?
【问题讨论】:
标签: node.js mongodb express mongoose