Skip to content

Instantly share code, notes, and snippets.

@hurrymaplelad
Created November 3, 2012 18:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hurrymaplelad/4008255 to your computer and use it in GitHub Desktop.
Save hurrymaplelad/4008255 to your computer and use it in GitHub Desktop.
Attempts to remove an attribute from mongo docs that isnt defined in the mongoose schema
var broccoli = new Food({
name: 'frozen broccoli',
organic: true
});
Food.collection.update({},
{$unset: {organic: true}},
{multi: true, safe: true},
function(err) {
Food.findById(broccoli, function(err, brocolli) {
console.log(broccoli.get('organic'));
}
);
> undefined
Food.update({},
{$unset: {organic: true}},
{multi: true, safe: true},
function(err) {
Food.findById(broccoli, function(err, broccoli) {
console.log(broccoli.get('organic'));
});
}
);
> true
Food.findById(broccoli, function(err, broccoli) {
console.log(broccoli.get('organic'));
});
> true
var Food = db.model('Food', new mongoose.Schema({
name: {type: String, required: true},
- organic: Boolean
}, {
strict: true
}));
broccoli.save(function() {
Food.findById(broccoli, function(err, broccoli) {
console.log(broccoli.get('organic'))
})
});
> true
var Food = db.model('Food', new mongoose.Schema({
name: {type: String, required: true},
organic: Boolean
}, {
strict: true
}));
Food.collection.update({},
{$unset: {organic: true}},
{multi: true, safe: true}
);
broccoli.set('organic', undefined);
broccoli.save(function() {
Food.findById(broccoli, function(err, broccoli) {
console.log(broccoli.get('organic'));
});
});
> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment