1

I wanted to delete 'user' entry from 'roleAssigned' array In the code:

Contact.update({ firstName:'Abhishek'},{$pull: { roleAssigned:'user'}});

This is what the Contact mongoDB model looks like:

[{
    roleAssigned: ['user', 'admin'],
    _id: 5b9b8ef904bc042c8f94f1fe,
    firstName: 'abhishek',
    lastName: 'soni',
    date: 2018-09-14T10:35:37.922Z,
    __v: 0
}]
6
  • can you post the schema please? Commented Sep 15, 2018 at 16:30
  • export const contactschema = new schema({ firstName: { type: String, required: 'enter your first name ' }, lastName: { type: String, required: 'enter your last name ' }, roleAssigned: { type: Array, default: [] }, email: { type: String }}) Commented Sep 15, 2018 at 16:40
  • "Abhishek" and "abhishek" both are different. Either use small a or use $regex in your above query. Something like this Contact.update({ firstName:{ $regex: 'Abhishek', $options: "i" }},{ $pull: { roleAssigned: 'user' }}) Commented Sep 15, 2018 at 16:46
  • i did the change but I am getting this error msg errmsg: 'Cannot apply $pull to a non-array value' Commented Sep 15, 2018 at 17:23
  • 1
    @AnthonyWinzlet yeah thanks it worked! Commented Sep 15, 2018 at 18:30

1 Answer 1

2

You can use the following solution to solve your problem:

{ $pullAll: { roleAssigned: ['user'] } };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.