2

I'm looking to add a new field (array that will be populated with $push in the future) to the current document but am getting the error update failed: MongoError: Cannot apply $push/$pushAll modifier to non-array

I'm working on the Meteor.users collection.

The code ran was:

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

2 Answers2

5

This happens because you're not supposed to change the root fields of the user object. From the docs:

By default, the current user's username, emails and profile are published to the client. You can publish additional fields with [...]

So you can

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

Note though that you should limit what you store in profile.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • This may not be the best advice if [you want it to sync](https://medium.com/@MaxDubrovin/workaround-for-meteor-limitations-if-you-want-to-sub-for-more-nested-fields-of-already-received-docs-eb3fdbfe4e07) – Michael Cole Sep 01 '15 at 19:09
1

You need to define "newfield" as an array or the operation will fail. See here: http://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push

TDmoneybanks
  • 478
  • 1
  • 7
  • 20