[英]How to remove element in mongodb array
How can I remove just one element of an array in a collection like this:如何仅删除集合中数组的一个元素,如下所示:
{
"_id" : "Y9BBFa4c4vMiAkjbi",
"metadata" : {
"tags" : [
"Anything",
"Something",
"More"
]
}
}
In this example I just want to remove 'Something' - if it is existing.在这个例子中,我只想删除“Something”——如果它存在的话。
var tag = 'Something';
if (Collection.find({ 'metadata.tags': tag }).count()) {
Collection.update(
{ _id: id },
{ $pull: { 'metadata.tags': tag } }
);
}
Just make sure your $pull
targets the array element you want to remove:只需确保您的
$pull
以您要删除的数组元素$pull
目标:
Collection.update({ _id: id },
{ $pull: { 'metadata.tags': { $eq: "Something" }}}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.