更新数组中蒙戈和UPSERT [英] Update array in mongo and upsert

查看:130
本文介绍了更新数组中蒙戈和UPSERT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新文档中的一个数组,它工作正常,但是当我要添加新的元素与UPSERT失败时如何运行错误。我一直在寻找对谷歌几个小时,MongoDB的文档,什么我都试过,我不能工作。

I'm trying to update an array within a document and it works correctly, but when I want to add a new element with upsert fails how to run an error. I've been searching on google for a few hours and the mongodb documentation and what I have tried I cannot operate.

集合的结构是:

{
"name" : String,
"providerId": Number,
"description": String,
"providers": [
    {
        "merchantId": String,
        "name": String,
        "valid": Boolean,
        "data": String
    },
    {
        "merchantId": String,
        "name": String,
        "valid": Boolean,
        "data": String
    },
    {
        "merchantId": String,
        "name": String,
        "valid": Boolean,
        "data": String
    }
]
}

使用这个查询更新现有数据:

Use this query to update existing data:

db.collection.update( { "providerId": ID, "providers": { $elemMatch: { "merchantId": MERCHANTID }}}, { $set: {

    "providers.$.merchantId": MERCHANTID,
    "providers.$.name": NAME,
    "providers.$.valid": true,
    "providers.$.data": DATA

}});

此工作正常和正确更新我的阵列的元素。我要当一个元素不存在添加它,没有如果有项目或不知道,但不能是,如果可能的话,探测器添加UPSERT({UPSERT:真})参数,但给了我下面的错误。我想,这是因为它不返回任何搜索对象。

This working properly and correctly updated me the elements of the array. I want to when an element does not exist add it, without knowing if there are items or not, but not is if possible, probe to add upsert ( { upsert: true } ) parameter but gives me the following error. I think it is because it does not return any object search.

这是错误:

MongoError:该位置经营者没有找到查询所需的匹配。未展开更新:。供应商$名称

有什么办法来更新数组中的子文档中的数据,并与增加新的兼容,如果他们不存在?我试着在操作$搜索和它给我的错误;还探索以不同的方式({providers.merchantId:MERCHANTID})进行搜索。和其他人

Is there any way to update the data in the subdocuments in the array and is compatible with add new ones if they don't exist? I've tried to search with the operator $in and it gives me error; also probe to search in different ways ( { "providers.merchantId": MERCHANTID } ) and others.

感谢

推荐答案

还有就是要实现你想要的一个选项。

There is an option to achieve what you want.

// step 1
var writeResult = db.collection.update({
    "providerId" : ID,
    "providers" : {
        $elemMatch : {
            "merchantId" : MERCHANTID
        }
    }
}, {
    $set : {
        "providers.$.merchantId" : MERCHANTID,
        "providers.$.name" : NAME,
        "providers.$.valid" : true,
        "providers.$.data" : DATA
    }
});


// step 2
if (!writeResult.nModified) { // if step 1 has succeeded on update, nModified == 1, else nModified == 0
    db.collection.update({
        "providerId" : ID,
        "providers.merchantId" : {
            $ne : MERCHANTID        // this criteria is necessary to avoid concurrent issue
        }
    }, {
        "$push" : {
            "prividers" : {
                "merchantId" : MERCHANTID,
                "name" : NAME,
                "valid" : true,
                "data" : DATA
            }
        }
    });
}

这篇关于更新数组中蒙戈和UPSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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