如何在 MongoDB 中的数组中提取项目的一个实例? [英] How to pull one instance of an item in an array in MongoDB?

查看:20
本文介绍了如何在 MongoDB 中的数组中提取项目的一个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文件:

$pull 运算符从现有数组中删除一个或多个与指定条件匹配的值的所有实例.

The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

是否可以选择仅删除值的第一个实例?例如:

Is there an option to remove only the first instance of a value? For example:

var array = ["bird","tiger","bird","horse"]

如何在更新调用中直接删除第一个鸟"?

How can the first "bird" be removed directly in an update call?

推荐答案

所以你是对的,$pull 运算符完全按照文档中的说明执行操作,因为它的参数实际上是用于匹配元素的查询"将被删除.

So you are correct in that the $pull operator does exactly what the documentation says in that it's arguments are in fact a "query" used to match the elements that are to be removed.

如果你的数组内容碰巧总是在你显示的第一个"位置有元素,那么 $pop 运算符实际上删除了第一个元素.

If your array content happened to always have the element in the "first" position as you show then the $pop operator does in fact remove that first element.

使用基本节点驱动程序:

With the basic node driver:

collection.findOneAndUpdate(
    { "array.0": "bird" },       // "array.0" is matching the value of the "first" element 
    { "$pop": { "array": -1 } },
    { "returnOriginal": false },
    function(err,doc) {

    }
);

与猫鼬返回修改后的文档的参数不同:

With mongoose the argument to return the modified document is different:

MyModel.findOneAndUpdate(
    { "array.0": "bird" },
    { "$pop": { "array": -1 } },
    { "new": true },
    function(err,doc) {

    }
);

但如果要删除的第一个"项的数组位置未知,则两者都没有多大用处.

But neither are of much use if the array position of the "first" item to remove is not known.

对于这里的一般方法,您需要两次"更新,一个是匹配第一个项目并将其替换为要删除的唯一内容,第二个是实际删除修改后的项目.

For the general approach here you need "two" updates, being one to match the first item and replace it with something unique to be removed, and the second to actually remove that modified item.

如果应用简单的更新而不要求返回的文档,这会简单得多,也可以跨文档批量完成.使用 async.series 之类的东西也有帮助,以避免嵌套调用:

This is a lot more simple if applying simple updates and not asking for the returned document, and can also be done in bulk across documents. It also helps to use something like async.series in order to avoid nesting your calls:

async.series(
    [
        function(callback) {
            collection.update(
                { "array": "bird" },
                { "$unset": { "array.$": "" } },
                { "multi": true }
                callback
            );
        },
       function(callback) {
           collection.update(
                { "array": null },
                { "$pull": { "array": null } },
                { "multi": true }
                callback
           );
       }
    ],
    function(err) {
       // comes here when finished or on error   
    }
);

所以使用 $unset 此处带有 positional $ 运算符允许将第一"项更改为 null.然后使用 $pull 的后续查询只是从数组中删除任何 null 条目.

So using the $unset here with the positional $ operator allows the "first" item to be changed to null. Then the subsequent query with $pull just removes any null entry from the array.

这就是你如何安全地从数组中删除第一次"出现的值.确定该数组是否包含多个相同的值是另一个问题.

That is how you remove the "first" occurance of a value safely from an array. To determine whether that array contains more than one value that is the same though is another question.

这篇关于如何在 MongoDB 中的数组中提取项目的一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆