Created
February 1, 2013 11:54
-
-
Save porsager/4690893 to your computer and use it in GitHub Desktop.
Mongoose: Updating values in null object results in mongo 10145 LEFT_SUBFIELD
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'), | |
_ = require('underscore'); | |
mongoose.connect('mongodb://localhost/test'); | |
mongoose.set('debug', true); | |
var docSchema = new mongoose.Schema({ | |
title: String, | |
meta: { | |
enabled: Boolean, | |
amount: Number, | |
note: String | |
} | |
}); | |
var Doc = mongoose.model('docs', docSchema); | |
function findAndSaveMeta() { | |
Doc.findOne({ title: "Test" }, function(err, doc){ | |
doc.meta = { enabled: true, amount: 1235, note: null }; | |
/* | |
//Setting mark modified will fix this issue. | |
doc.markModified('meta'); | |
*/ | |
doc.save(function(err, doc){ | |
console.log(err); // Gives LEFT_SUBFIELD only supports Object | |
Doc.remove(); // Remove test docs | |
}); | |
}); | |
} | |
function findAndSaveNull() { | |
Doc.findOne({ title: "Test" }, function(err, doc){ | |
doc.meta = null; | |
doc.save(function(err, doc){ | |
findAndSaveMeta(); | |
}); | |
}); | |
} | |
var testSave = new Doc({ title: "Test", meta:null }); | |
testSave.save(function(err, doc){ | |
findAndSaveNull(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment