mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Removing embedded documents failing - known issue?

Open jmwhittaker opened this issue 15 years ago • 11 comments

The docs don't make this clear as to how you correctly remove embedded documents.

This fails to correctly remove the embedded document correctly when I find the entry using the mongodb console (example from the doc's)

BlogPost.findById(myId, function (err, post) {
  if (!err) {
    post.comments[0].remove();
    post.save(function (err) {
      // do something
    });
  }
});

Looks like i'm not the only one with issues when trying to remove or update embedded documents http://groups.google.com/group/mongoose-orm/browse_thread/thread/a0c81b3a178a6205

From the code (array.js) the comment reads:

/**
 * Removes items from an array atomically
 *
 * Examples:
 *     doc.array.remove(ObjectId)
 *     doc.array.remove('tag 1', 'tag 2')
 *
 * @param {Object} value to remove
 * @api public
 */

Neither of these examples work.

Any ideas how to get this working?

jmwhittaker avatar Apr 30 '11 19:04 jmwhittaker

i am having the same issue and it's stalling my development. any workaround?

ruqqq avatar May 01 '11 05:05 ruqqq

to add on, i tried:

1. doc.items[i].remove();
2. splicing as suggested here: http://stackoverflow.com/questions/5316709/mongoose-wont-remove-embedded-documents
3. delete doc.items[i];

But nothing works! Most of the time the save returns a success but the changes weren't committed to the db.

ruqqq avatar May 01 '11 05:05 ruqqq

After much debugging, I found out that the version of mongodb I was running was 1.2+. Upgrading to 1.6+ from the Ubuntu repo provided on mongodb website fixes this issue. jmwhittaker, perhaps check your mongodb version.

ruqqq avatar May 01 '11 06:05 ruqqq

I have MongoDB version 1.8.0 (installed via Homebrew) and i'm getting these issues.

jmwhittaker avatar May 01 '11 11:05 jmwhittaker

remove() works on my machine, and it also passes a series of tests in the test directory.

Can you do the following for me? Run

$ make test

from the mongoose npm directory or cloned mongoose directory. Let me know if all the tests pass (with the exception of 1 test, for compound indexes). If they all (but one) pass, then it could be a problem with the way you are using mongoose in your app, in which case make a https://gist.github.com that is a fully executable example that fails on your machine that I can just download and run to re-produce the issue on my machine.

bnoguchi avatar May 03 '11 09:05 bnoguchi

I'm experiencing the same issue with mongo 1.8.0 from macports and seeing some errors from the tests.

Node version: v0.4.6 Mongo version: db version v1.8.0, pdfile version 4.5

   document.test.js test shortcut getters: AssertionError:  === {"cool":"4cf70857337498f95900001c","age":2}
    at /Users/heikki/workspace/mongoose/test/document.test.js:86:12
    at next (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:770:25)
    at runSuite (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:788:6)
    at check (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:695:16)
    at runFile (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:699:10)
    at Array.forEach (native)
    at runFiles (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:676:13)
    at run (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:645:5)
    at Object.<anonymous> (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:874:13)
    at Module._compile (module.js:404:26)


 You're not testing for replica sets! 
 Please set the MONGOOSE_SET_TEST_URI env variable. 
 e.g: `mongodb://localhost:27017/db,mongodb://localhost…` 


   uncaught: TypeError: Cannot read property 'documents' of null
    at /Users/heikki/workspace/mongoose/support/node-mongodb-native/lib/mongodb/db.js:561:24
    at [object Object].executeCommand (/Users/heikki/workspace/mongoose/support/node-mongodb-native/lib/mongodb/db.js:717:16)
    at [object Object].lastError (/Users/heikki/workspace/mongoose/support/node-mongodb-native/lib/mongodb/db.js:560:8)
    at [object Object].error (/Users/heikki/workspace/mongoose/support/node-mongodb-native/lib/mongodb/db.js:566:8)
    at [object Object].insertAll (/Users/heikki/workspace/mongoose/support/node-mongodb-native/lib/mongodb/collection.js:170:15)
    at [object Object].insert (/Users/heikki/workspace/mongoose/support/node-mongodb-native/lib/mongodb/collection.js:67:8)
    at Array.<anonymous> (/Users/heikki/workspace/mongoose/lib/mongoose/drivers/node-mongodb-native/collection.js:65:25)
    at EventEmitter._tickCallback (node.js:126:26)


   uncaught: AssertionError: expected false to be true
    at Object.true (/Users/heikki/workspace/mongoose/support/should.js/lib/should.js:225:10)
    at Object._onTimeout (/Users/heikki/workspace/mongoose/test/model.test.js:3470:23)
    at Timer.callback (timers.js:83:39)

┏━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Stat                  ┃ Time (ms)     ┃
┣━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━┫
┃ Queries run           ┃ 578           ┃
┣━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━┫
┃ Time ellapsed         ┃ 1335          ┃
┣━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━┫
┃ Connections opened    ┃ 268           ┃
┣━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━┫
┃ Connections closed    ┃ 268           ┃
┗━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┛

   collection.test.js test buffering of commands until connection is established: AssertionError: expected false to be true
    at Object.true (/Users/heikki/workspace/mongoose/support/should.js/lib/should.js:225:10)
    at /Users/heikki/workspace/mongoose/test/collection.test.js:28:26
    at Array.0 (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:773:37)
    at EventEmitter.<anonymous> (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:804:35)
    at EventEmitter.<anonymous> (events.js:81:20)
    at EventEmitter.emit (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:869:10)
    at report (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:815:13)
    at EventEmitter.emit (/Users/heikki/workspace/mongoose/support/expresso/bin/expresso:866:9)


   Failures: 4


make: *** [test] Error 4

heikkiv avatar Jun 08 '11 08:06 heikkiv

Yes, these tests should be failing on the latest master. They're failing tests that we added for certain issues, and we're planning on updating the code to make them pass. That said, the tests that fail are not related to removing embedded documents. We do have a test for removing an embedded document pass (Ctrl-f for "test for removing a subdocument atomically" in ./test/model.test.js). And this test does pass.

Please submit a gist that can re-produce this issue. Until then, I'm closing this issue.

bnoguchi avatar Jun 08 '11 11:06 bnoguchi

@bnoguchi, I too am having this issue. I'm on Mac OS 10.6.8, Mongoose v1.5.0, node v0.4.9, mongodb 1.8.2-x86_64 (installed via homebrew). I have put my code, as well as the resultant error trace, into this gist. Do you have any insight into what's going wrong? Thanks very much!

sebm avatar Jun 29 '11 22:06 sebm

@sebm Your code is not written in an async fashion. That, combined with the way that var scoping works, means that you are manipulating things that are not what you think they are. Please refer to my gist for a correct implementation of your example.

bnoguchi avatar Jul 01 '11 23:07 bnoguchi

@sebm, I think there is a legitimate bug here. Please see my gist. The issue, at least the one I'm having, seems to happen when removing more than one embedded document.

I've found that if I remove only one embedded document and then save, it works just fine. But as soon as I try to remove more than one and then save, the modifications are not persisted.

Would you like me to create a new issue to track this if this is in fact an issue?

EDIT: I'm on OSX 10.6.8, MongoDB 1.6.5-x86_64 and Mongoose ver. 2.1.2. I just realized I'm not on the latest version of MongoDB, so I'll update and retry.

bentruyman avatar Sep 11 '11 16:09 bentruyman

I'm also having this problem on MacOS X, latest Mongo, latest mongoose: if I perform one sub-document .remove() and save immediately it works. If I perform two or more, the driver believes it works, and even reports back the properly modified model, but Mongo does not reflect the changes on inspection. 100% reproducible.

Please see my gist.

jcworsley avatar Nov 17 '11 00:11 jcworsley