Created
January 19, 2013 23:19
-
-
Save mseegers/4575777 to your computer and use it in GitHub Desktop.
Testing pull command on sub document array in mongoose 3.5.4. There seems to be an order dependent issue when making other modifications to the same sub document array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
mongoose.set('debug', true); | |
var mongooseConnection = mongoose.createConnection('mongodb://localhost/mongoosepulltest'); | |
var SubDocSchema = new mongoose.Schema({ | |
someValue: { type: Number }, | |
},{ | |
versionKey: false | |
}); | |
var DocSchema = new mongoose.Schema({ | |
someId: { type: Number }, | |
subDocs: [SubDocSchema], | |
}); | |
var Doc = mongooseConnection.model('Doc', DocSchema) | |
var main = new Doc({ | |
someId: 1, | |
subDocs: [{ someValue: 1 },{ someValue: 2 },{ someValue: 3 }] | |
}); | |
main.save(function(err) { | |
if (err) { return console.log(err); } | |
Doc.findOne({someId: 1}, function(err, doc){ | |
if (err) { return console.log(err); } | |
// This works fine | |
//var mySub = doc.subDocs[2]; | |
//doc.subDocs.pull(doc.subDocs[1]); | |
//mySub.someValue = 5; | |
// This will stomp the = 5 change. | |
var mySub = doc.subDocs[2]; | |
mySub.someValue = 5; | |
doc.subDocs.pull(doc.subDocs[1]); | |
doc.save(function(err) { | |
if (err) { return console.log(err); } | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment