Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot update embedded documents #261

Closed
lukegalea opened this issue Feb 20, 2011 · 7 comments
Closed

Cannot update embedded documents #261

lukegalea opened this issue Feb 20, 2011 · 7 comments

Comments

@lukegalea
Copy link

I can't find any way to update the contents of an embedded document once it's been saved. I've tried modifying the embedded doc and saving the parent.. saving the embedded doc itself, etc.. Nothing works. Here's some code to reproduce:

var EmbeddedDocSchema = new Schema({
  inner_label: String
});

mongoose.model('EmbeddedDoc', EmbeddedDocSchema);
var EmbeddedDoc = mongoose.model('EmbeddedDoc');

var ObservationSchema = new Schema({
  time_label: String,
  embedded_docs: [EmbeddedDocSchema]
});

mongoose.model('Observation', ObservationSchema);
var Observation = mongoose.model('Observation');

var o = new Observation();
o.embedded_docs.push({inner_label: "Initial Value"}); 
o.save(function(err){
  //Now let's try to modify this and save again
  var embedded = o.embedded_docs[0];
  embedded.inner_label = "A modified value";
  embedded.save(function(err) { //o.save makes no difference
    console.dir(o.embedded_docs[0].inner_label); // That's nice, it appears to have been updated
    Observation.findById(o._id, function(err, reloadedObservation) {
      console.dir(reloadedObservation.embedded_docs[0].inner_label); // Dang! It's still set to "Initial Value"
    });
  });
});
@kof
Copy link

kof commented Feb 25, 2011

@kof
Copy link

kof commented Feb 26, 2011

ok, we hve not the same issue:

now I know what the problem is:

you can't update schemafree embedded documents!

Here is a clear example how to reproduce it:

https://gist.github.com/845258

run this:

this will create data in the db

node create

go then to mongo shell:

use mymodel

db.mymodels.find()

{ "_id" : ObjectId("4d690e72271839392d000001"), "name" : "test",
"elements" : [ { "name" : "schemafree" } ] }

this should modify the data

node modify

db.mymodels.find()

{ "_id" : ObjectId("4d690e72271839392d000001"), "name" : "test",
"elements" : [ { "name" : "schemafree" } ] }

The question now is it an issue or a missing feature?

@rakeshpai
Copy link

+1'ing this issue. It seems to be occurring with the Number type at least, even on the main document (not just embedded). Would be awesome if someone at least has a workaround at least for the moment.

[Edit] Doesn't seem to happen with strings. Only Numbers.

@bnoguchi
Copy link
Contributor

bnoguchi commented Jun 8, 2011

@kof, @lukegalea

This is a missing feature -- i.e., being able to call save directly on an embedded document. I'm going to add this as a separate issue.

save must currently be called on the top level document. Therefore, the correct version of the original Issue's example should be re-written so that the following line changes from:

  embedded.save(function(err) { //o.save makes no difference

to

  o.save(function(err) { //o.save makes no difference

@rakeshpai Not sure what you mean. Can you review my comments, and let me know if you are still having an issue? If you still are, please be descriptive and attach an example or gist to your reply.

@bnoguchi bnoguchi closed this as completed Jun 8, 2011
bnoguchi added a commit that referenced this issue Jun 8, 2011
@lukegalea
Copy link
Author

@bnoguchi Thanks. I'm prevented from testing this out in my app by a new problem introduced in 1.3.x. I just logged it as #377

@rakeshpai
Copy link

Sorry for the delayed response. I discovered that my problem is a dupe of #342

@neekey
Copy link

neekey commented Oct 7, 2011

@bnoguchi do you mean that this issue is because 'a single instantiated document only be able to update its embedded documents once'? is the latest version of mongoose fixed this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants