Skip to content

Instantly share code, notes, and snippets.

@porsager
Created February 1, 2013 11:54
Show Gist options
  • Save porsager/4690893 to your computer and use it in GitHub Desktop.
Save porsager/4690893 to your computer and use it in GitHub Desktop.
Mongoose: Updating values in null object results in mongo 10145 LEFT_SUBFIELD
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