-1

I have been trying to update a collection i have.Problem is,i dont know the field name so am relying on some logic to come up with the field name.

For instance

  Er.update({ _id: "BCwrQEdiTr9GMmKWW" }, {
    $set: {
      "x" : y
    }
  });

where x is the field name.

This is my code

var obj = {};
obj[x] = y;

Er.update({ _id: "BCwrQEdiTr9GMmKWW" }, {$set: {obj}});

I am getting this error

update failed: MongoError: The dotted field 'ersubjects.0.English' in 'obj.ersubjects.0.English' is not valid for storage.

English is a field under ersubjects so i want to update it this way ersubjects.0.English and it works on mongo.

Why is this not working in meteor?.

Július Retzer
  • 1,055
  • 1
  • 10
  • 25
Le Qs
  • 785
  • 2
  • 9
  • 26

1 Answers1

0

You can't store documents that have a dot in the key. See this answer for an explanation.

What you can do is use lodash's extremely handy _.set function to create your object with dynamic keys like this:

var obj = {};
var variableKey = 'ersubjects';
_.set(obj, [variableKey, 0, 'English], 'someValue');

Now you can safely store this object to Mongo.

Community
  • 1
  • 1
Július Retzer
  • 1,055
  • 1
  • 10
  • 25
  • Complains set is not a function. – Le Qs May 14 '16 at 22:51
  • What version of lodash are you using? `_.set` is available from version 3.7.0 – Július Retzer May 14 '16 at 22:55
  • OMG,i just installed 0.7.1,which is a bit weird coming from what looks like their official lodash distro on atmosphere `alethes:lodash` – Le Qs May 14 '16 at 23:02
  • https://atmospherejs.com/stevezhu/lodash this is the most used package on atmosphere. As a side note, I would recommend using meteor 1.3 and move from atmosphere to npm packages asap. No more problems like this :) – Július Retzer May 14 '16 at 23:04
  • I will switch to npm soon. Using lodash in helpers is proving problematic,even after installing lodash. – Le Qs May 14 '16 at 23:10
  • underscore to lodash will be a nuclear blast for my project , https://github.com/rclai/meteor-lodash-replace-underscore as i am also using iron router – Le Qs May 14 '16 at 23:18