我有一个看起来像这样的猫鼬模式,我可以很好地更新和查询:
// Subscriber Schema
var Subscriber = new Schema({
'user': ObjectId,
'level': { type: String, enum: [ 'sub', 'poster', 'blocked' ] }, //blocked means users has opted out of list
'dateAdded': { type: Date, default: Date.now }
});
// Group Schema
var Group = new Schema({
'groupEmail': { type: String, lowercase: true, validate: [validatePresenceOf, 'an email is required'], index: { unique: true } },
'groupOwner': ObjectId,
'groupName': String,
'twitterFeed': String,
'subscribers': [Subscriber],
'createdAt': { type: Date, default: Date.now }
});
我正在尝试更新组文档中的订阅者,并更改其级别,然后保存更改。我可以使用以下方法找到该组:
Group.findOne({ _id: req.params.groupId }, function(err, g)
拥有该组后,我想找到一个订阅者以便更新它。如果我尝试:
g.subscribers.update({ 'user': req.params.userId }, { level: newStatus });
我得到:
TypeError: Object { user: 4fc53a71163006ed0f000002,
level: 'sub',
_id: 4fd8fa225904a5451c000006,
dateAdded: Wed, 13 Jun 2012 20:37:54 GMT },{ user: 4fda25ac00cd9bdc4f000004,
level: 'sub',
_id: 4fda270bbce9f8d058000005,
dateAdded: Thu, 14 Jun 2012 18:01:47 GMT },{ user: 4fda2a634499dfd16e00000d,
level: 'sub',
_id: 4fda2a634499dfd16e00000e,
dateAdded: Thu, 14 Jun 2012 18:16:03 GMT } has no method 'update'
我尝试了更新、查找等的各种排列,但总是得到“没有方法”错误。我究竟做错了什么?我应该以不同的方式更新 Subscriber.level 吗?
谢谢!!