Note that there are some explanatory texts on larger screens.

plurals
  1. PONodeJS/Mongoose/MongoDB - Pull (from array) not working
    primarykey
    data
    text
    <p>I am trying to pull/remove values from an array (subdocument).</p> <p>Example doc:</p> <pre><code>{ _id: 5150a1199fac0e6910000002, name: 'some name, items: [{ id: 23, name: 'item name 23' },{ id: 24, name: 'item name 24' }] } </code></pre> <p>I have the doc in memory as I pulled it from mongo already. I am trying:</p> <pre><code>var left = object.items.pull({id: 24}); console.log(left) </code></pre> <p>Outputs:</p> <pre><code>[{ id: 23, name: 'item name 23', _id: 5150a1199fac0e6910000045 },{ id: 24, name: 'item name 24', _id: 5150a1199fac0e6910000002 }] </code></pre> <p>So looks like I already have a problem. The array returned from the pull call should be whats left in the array after the pull . Not sure why it did not pull from the item array. Does this only support pulling by ObjectId ant not user defined fields?</p> <p>I call save:</p> <pre><code>object.save(); </code></pre> <p>Mongoose logs this as the query:</p> <pre><code>Mongoose: mycollection.update({ _id: ObjectId("5150901ac345824a07000002"), __v: 3 }) { '$inc': { __v: 1 }, '$pull': { attachments: { _id: { '$in': [ ObjectId("5150c64d63773efb1f000002") ] } } } } {} </code></pre> <p>This shows why my document is not being removed. The _id of the array item that should be removed is:</p> <pre><code>_id: 5150a1199fac0e6910000002 </code></pre> <p>The item that mongoose is sending to the database to remove is:</p> <pre><code>ObjectId("5150c64d63773efb1f000002") </code></pre> <p>Any ideas what I am doing wrong? I am trying to delete by something other than _id, not sure if that is possible but from the docs looks to be.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload