从Collection内的数组中删除对象

时间:2015-03-09 18:14:22

标签: arrays mongodb meteor

我正在使用以下方法将对象推送到“跟随”数组,这是每个用户配置文件的属性。

Meteor.users.update({ _id: this.userId }, { $push: { 
  "profile.following": { _id: _id, service: service, type: type }}
 });

// I specify that it is required to match these two properties to remove an object
Meteor.users.update({ _id: this.userId }, { $pull: { 
  "profile.following": { service: service, type: type } }
});

这种方法确实有效,它会删除对象,但我总是会收到此错误:

Exception while simulating the effect of invoking 'unfollow' Error: documentMatches needs a document {stack: (...), message: "documentMatches needs a document"} Error: documentMatches needs a document
at Error (native)
at _.extend.documentMatches (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:1341:13)
at MODIFIERS.$pull (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:3414:24)
at http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:3124:9
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:3105:9
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at Function.LocalCollection._modify (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:3098:7)
at LocalCollection._modifyAndNotify (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:848:19)
at http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:765:12

我不确定这是Meteor的错误,还是我可能没有正确编写查询。

1 个答案:

答案 0 :(得分:1)

执行此操作需要显式$elemMatch

Meteor.users.update({ _id: this.userId }, { $pull: { 
  "profile.following": { $elemMatch: { service: service, type: type } } }
});