2

我正在寻找向当前文档添加一个新字段(将来将使用 $push 填充的数组)但出现错误update failed: MongoError: Cannot apply $push/$pushAll modifier to non-array

我正在研究 Meteor.users 集合。

运行的代码是:

var user = Meteor.userId();
Meteor.users.update({_id:user}, {$set: {"newfield": ["some data"]}});
4

2 回答 2

5

发生这种情况是因为您不应该更改用户对象的根字段。从文档

默认情况下,当前用户的usernameemails发布profile到客户端。您可以使用 [...] 发布其他字段

这样你就可以

Meteor.users.update(user, {$set: {"profile.newfield": ["some data"]}});

请注意,您应该限制存储profile.

于 2015-02-10T22:21:26.807 回答
1

您需要将“newfield”定义为数组,否则操作将失败。见这里:http ://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push

于 2015-02-10T22:37:52.880 回答