0

I have a nested array of documents in meteor mongo like so:

{
"_id" : "XBsaRPrangiS7xmAc",
"chartData" : [
    {
        "sectionID" : ObjectId("74d5541d775e4836478edff7"),
        "sectionName" : "abc",
        "sectionData" : [more nested documents...]
    },
    {
        "sectionID" : ObjectId("c17841e1b2e46a5f4f43c41d"),
        "sectionName" : "def",
        "sectionData" : [more nested documents...]
    }
    ...more sections...
]
}

Based on several stackoverflow answers, I have tried to update fields (eg. chartData.sectionName) like so on the server:

  ChartData.update({
    '_id':'XBsaRPrangiS7xmAc',
    'chartData.sectionID': new Mongo.ObjectID("74d5541d775e4836478edff7")
  },{
    '$set': { 'chartData.sectionName': 'hello'}
  });

Now I'm getting an error message which says: "Exception while invoking method 'updateSectionName' MongoError: cannot use the part (chartData of chartData.sectionName) to traverse the element".

What's wrong with my code and what is the proper way to update nested documents in meteor?

Poh Zi How
  • 1,489
  • 3
  • 16
  • 38
  • 1
    You need to include the positional operator `$`. Try `'$set': { 'chartData.$.sectionName': 'hello'}`. – s7vr Feb 12 '17 at 16:00
  • @Veeram I read somewhere that the positional operator doesn't work in meteor, but guess thats only on the client side. Thank you so much! – Poh Zi How Feb 12 '17 at 16:05

0 Answers0