I've been having trouble with the problem of removing a string from an array in a mongodb document. For example I want to remove the entry "four" from the list field in the document below.
{
"list" : [ "1", "2", "four", "five", "6"]
}
I know it seems very simple but I haven't been able to find a straight answer from the docs or previous questions. I've tried 5 or so commands to no avail using db.collection.update combined with the $pull and $mod modifiers. Any help?
I know, very rudimentary question.
You can use $Pull operator, please try the below query :
db.collection.update(
{ _id : id },
{ $pull:
{ "list":"four" }
}
);
If you want to remove two or more elements from the arry "list", you can do that as well with $Pull operator as well :
db.collection.update( { _id : id },
{ $pull:
{ list :
{ $in : [ "one", "four" ] }
}
}
);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments