mongodb - Meteor/Mongo nested arrays update -


i'm new meteor/mongo/js stack , i'm getting lost in json arrays , referencing them. based another answer ( , docs) think close...

a document in orders collection, document has nested arrays.

order -> orderlines -> lineitems: 

sample doc:

{ "_id" : "27tgprtmwyqppfkdn", "orderlines" : [     {         "lineid" : 1,         "name" : "cheese & pickle",         "instructions" : "",         "lineitems" : [             {                 "name" : "cheddar cheese",                 "quantity" : 1             },             {                 "name" : "branston pickle",                 "quantity" : 1             },             {                 "name" : "focaccia roll",                 "quantity" : 1             }         ]     }   ] } 

what i'm trying meteor/mongo shell:

  1. add "instructions" of "foo" orderlines lineid = 1
  2. put new item on lineitems array

this appears hang...

meteor:primary> db.orders.update({_id:"27tgprtmwyqppfkdn","orderlines.lineid":"1", {$set: {"orderlines.$.instructions":"foo"}}) ...  

this doesn't identifier in query

meteor:primary> db.orders.update({_id:"27tgprtmwyqppfkdn", "orderlines.lineid":"1"}, {$push:{"orderlines.$.lineitems":" { "name" : "butter", "quantity" : 1}"}}); 2015-10-27t16:09:54.489+0100 syntaxerror: unexpected identifier 

thanks comments... found out answers, posted reference

item 1 - using $set on value within array

this failing due 2 typos, 1 missing closing } @ end of query, second quoting value "1" itemid in query.

this works:

 db.orders.update({_id:"27tgprtmwyqppfkdn", orderlines.lineid":1},   {$set: {"orderlines.$.instructions":"foo"}}) 

i realised when said "it appears hang" cli waiting valid statement, hints @ missing } or )!

item 2 - using $push add data array - 2 levels nested

this failing due quoting around array data

db.orders.update({_id:"27tgprtmwyqppfkdn", "orderlines.lineid":1 },  {$push:{"orderlines.$.lineitems": { "name" : "butter", "quantity" : 1} }}) 

nested arrays: can use $ positional operator

what want next use $set on item in second level array, , require using $ positional operator twice:

db.orders.update({"orderlines.lineid":1, lineitems.name:"cheddar cheese"}, {$set: {"orderlines.$.lineitems.$.quantity": 2}}) 

this throws error:

too many positional (i.e. '$') elements found in path 

there open mongodb enhancement request this it's been open since 2010


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -