Lets say I have the following schema in Mongoose:
var OrganisationSchema = mongoose.Schema({
name: { type: String },
users: [ UserSchema ]
});
var UserSchema = mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true }
});
And I saw this example in the docs of mongoose
doc.array.pull(ObjectId)
doc.array.pull({ _id: 'someId' })
doc.array.pull(36)
doc.array.pull('tag 1', 'tag 2')
Therefore I wonder why this function does not work:
OrgSchema.methods.removeUser = function(mail, onRemoveError, onRemoveSucess) {
var org = this;
org.users.pull({email: mail});
org.save(function(err) {
if(err) {
onRemoveError(err);
} else {
onRemoveSuccess(org); // Gets called but has not removed the user
}
});
};
This kind of pull does not work and I wonder why? Most of the question threads here on stackoverflow refer to the mongodb pull method for doing this sort of thing:
Org.update( { _id: orgid }, { $pull: { candidates: { email: mail }}});
Is this the correct way to go? Can´t I pull directly on a document array?
org.usersjust afterorg.users.pull(). Is your change taking place?org.candidates.pull(id). I could use id instead but not the query:{email: mail}