Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.