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

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

问题描述

根据文档:

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

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

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

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

解决方案

所以您是正确的,因为 $pull 运算符完全按照文档中的说明进行操作,因为它的参数实际上是用于匹配要删除的元素的查询". /p>

如果您显示的数组内容碰巧总是将元素置于第一"位置,则 $pop 运算符实际上确实删除了第一个元素.

使用基本节点驱动程序:

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

    }
);

对于猫鼬来说,返回修改后的文档的参数是不同的:

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

    }
);

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

在这里,对于一般方法,您需要两次"更新,一次更新要匹配第一个项目,然后用唯一要删除的东西替换它,而第二次要实际删除该修改过的项目.

如果应用简单更新而不要求返回返回的文档,则这要简单得多,并且还可以跨文档批量进行.使用 async.series 之类的东西也可以避免嵌套调用: /p>

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 在此处,通过位置$ 运算符, 第一个"项目将更改为null.然后,使用$pull进行的后续查询只会从数组中删除所有null条目.

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

According to the documents:

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?

解决方案

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.

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.

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   
    }
);

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天全站免登陆