1

I am using mongoose in my node app and i have a colleciton as follows,

enter image description here

when i update the status of seating it returns an error saying,

{ MongoError: cannot use the part (seating of ticket.seating.attachment.0.enabled) to traverse the element ({seating: [ { ticketUrl:

My Code

 return dbService.findOneAndUpdate(Model, {
       'listingId': attachmentObj.listingId,
        'ticket.seating.attachment.attachmentUrl': attachmentObj.attachmentUrl
           }, {
        $set: {
       'ticket.seating.attachment.$.enabled': true
      }
 })
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • `seating` is an embedded array, to specify an element inside, you should specify the index there as well. – S.D. Apr 25 '17 at 07:20
  • Yes. But I doubt `$` will have seating index as well as attachment index. – S.D. Apr 25 '17 at 07:23
  • that throws an error say MongoDB: Too many positional (i.e. '$') elements found in path – Sajeetharan Apr 25 '17 at 07:24
  • Possible duplicate question: http://stackoverflow.com/questions/18573117/updating-nested-arrays-in-mongodb-via-mongo-shell – itsundefined Apr 25 '17 at 07:28
  • $(positional) element works only once, so you cant use it for multiple indices. – Ravi Shankar Bharti Apr 25 '17 at 07:29
  • 2 level nested arrays will be hard to update by index. And for that mongoose generates `_id` for each embedded document. You can try updating it using mongoose get and save on the document. – S.D. Apr 25 '17 at 07:30

0 Answers0