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

Already on GitHub? Sign in to your account

embedded document is pushed every time I call "save" #267

Closed
borbit opened this Issue Feb 23, 2011 · 6 comments

Comments

Projects
None yet
6 participants

borbit commented Feb 23, 2011

var TestEmbeddedSchema = new Schema({
    value: String
});

var TestSchema = new Schema({
    value   : String,
    embedded : [TestEmbeddedSchema]
});

mongoose.model('Test', TestSchema);

var Test = mongoose.model('Test');

var doc = new Test();

doc.value = 'test';
doc.embedded.push({value: 'test'});
doc.save(function() {
    doc.save(function() {
        process.exit();
    });
});

Every time I call save the same embedded doc is pushed.

So I have this in a db:

{ "_id" : ObjectId("4d64df4f38ab9c6a13000001"), "embedded" : [
    {
        "_id" : ObjectId("4d64df4f38ab9c6a13000002"),
        "value" : "test"
    },
    {
        "_id" : ObjectId("4d64df4f38ab9c6a13000002"),
        "value" : "test"
    }
], "value" : "test" }

Best regards.

After saving your document the first time, don't work with "doc". Use a findOne or findById to pull the record from the database and work with that saved object instead. That should solve your problem.

Paden commented Mar 11, 2011

I tried what you said, hayama, and still the whole nested document is updated.

Any other workarounds?

narf commented Mar 21, 2011

Same problem here, the ORM should take care of the "saving twice" problem.

For a workaround, the solution given by hayama should work fine look at the output of the code (just copy paste it and execute with node):

var mongoose = require('mongoose');  
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/testDb');


var TestEmbeddedSchema = new Schema({
    value: String
});

var TestSchema = new Schema({
    value   : String,
    embedded : [TestEmbeddedSchema]
});

mongoose.model('Test', TestSchema);

var Test = mongoose.model('Test');

var doc = new Test();
doc.value = 'test';
doc.embedded.push({value: 'test'});
console.log('Saving: ' + JSON.stringify(doc));
doc.save(function() {
    console.log('Saving again: ' + JSON.stringify(doc));
    doc.save(function() {
        console.log('After two saves: ' + JSON.stringify(doc));
        Test.findById(doc._id, function (error, recoveredTest) {
            console.log('Found: ' + JSON.stringify(recoveredTest));

            console.log('');
            doc = new Test();
            doc.value = 'test';
            doc.embedded.push({value: 'test'});
            console.log('Saving: ' + JSON.stringify(doc));
            doc.save(function() {
                Test.findById(doc._id, function (error, recoveredDoc) {
                    console.log('Saving again: ' + JSON.stringify(recoveredDoc));
                    recoveredDoc.save(function() {
                        console.log('After two saves: ' + JSON.stringify(recoveredDoc));
                        Test.findById(doc._id, function (error, recoveredTest) {
                            console.log('Found: ' + JSON.stringify(recoveredTest));

                            mongoose.connection.close();
                        });
                    });
                });
            });
        });
    });
});

wavded commented Mar 22, 2011

I'm also encountering this issue. Another work around is only to save at the end.

Contributor

bnoguchi commented Mar 23, 2011

Closed by 87c3a4c. No longer sending embedded doc $push to mongodb twice if you save a new doc twice. Atomics and dirty attributes now properly cleared after each save.

@bnoguchi bnoguchi closed this Mar 23, 2011

Contributor

bnoguchi commented Mar 23, 2011

Just pushed a fix. You can grab master, or you can wait for the next imminent version bump.

aheckmann pushed a commit to aheckmann/mongoose that referenced this issue May 17, 2011

aheckmann pushed a commit to aheckmann/mongoose that referenced this issue May 17, 2011

Fixed GH-267. No longer sending embedded doc $push to mongodb twice i…
…f you save a new doc twice. Atomics and dirty attributes now properly cleared after each save.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment