如何从mongodb文档中的数组中删除字符串? [英] How do I remove a string from an array in a mongodb document?
问题描述
我一直在解决从mongodb文档中的数组中删除字符串的问题.例如,我想从下面的文档的列表字段中删除条目四".
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"]
}
我知道这似乎很简单,但我无法从文档或先前的问题中找到直接答案.我已经尝试使用db.collection.update结合$ pull和$ mod修饰符对5个左右的命令无效.有帮助吗?
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.
推荐答案
您可以使用 $ Pull 运算符,请尝试以下查询:
You can use $Pull operator, please try the below query :
db.collection.update(
{ _id : id },
{ $pull:
{ "list":"four" }
}
);
如果要从arry列表"中删除两个或多个元素,也可以使用$ Pull运算符来完成:
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" ] }
}
}
);
这篇关于如何从mongodb文档中的数组中删除字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!