使用php mongo执行命令显示错误"异常:无法执行。在字段名称"和错误代码:16722

时间:2015-04-11 18:14:32

标签: javascript php mongodb execute

我正在尝试使用php mongo execute command运行一组java脚本和更新命令:

$pSsId                      = '123456789';
$pUid                       = 14;
$pRowID                     = '6fce077519d838bb8ed401448dae6e3a';
$pKey                       = 'name';
$pValue                     = 'King Kobra';
$response                   = $db()->execute("
        function(pSsid, pUid, pRowid, pKey, pValue){
            udocs    = db.VizSpreadsheet.findOne({'_id' : pSsid, 'data.uid' : pUid  }).data; 
            posU     = udocs.map(function(d) { return d.uid; }).indexOf(pUid);
            posR     = udocs[posU].rows.map(function(r) { return r.row_id; }).indexOf(pRowid);

            var setCriteria = {};
            setCriteria['_id'] = pSsid;
            setCriteria['data.uid'] = pUid;

            var setObject = {};
            setObject['data.'+posU+'.'+'rows'+'.'+posR+'.'+pKey] = pValue;

            db.VizSpreadsheet.update(
                {
                    setCriteria
                },
                {
                    '$set': setObject
                }
            );
        }", array($pSsId, $pUid, $pRowID, $pKey, $pValue));

但结果是错误

Array ( [errmsg] => exception: can't have . in field names [data.1.rows.0.2#12#07337187ee7e48f92ed1689b22d7ed77] at src/mongo/shell/collection.js:155 [code] => 16722 [ok] => 0 ) 

该集合将如下所示

"_id" : "123456789",
"data" : [ 
    {
        "uid" : 12,
        "rows" : [ 
            {
                "row_id" : "8979afefedb42aa8c62e9baa83e35ba0",
                "updated_by" : "12",
                "updated_at" : "1428644989",
                "name" : "AAAAAAAAAAA"
            }, 
            {
                "row_id" : "7415f767c62a84173d1dcf82ad1d809d",
                "updated_by" : "12",
                "updated_at" : "1428644989",
                "name" : "BBBBBBBBBBBBB" 
            }
        ]
    }, 
    {
        "uid" : 14,
        "rows" : [ 
            {
                "row_id" : "21b1120811cfe893486e9e9afbebb660",
                "updated_by" : "12",
                "updated_at" : 1428644989,
                "name" : "CCCCCCCCCC"
            }, 
            {
                "row_id" : "6fce077519d838bb8ed401448dae6e3a",
                "updated_by" : "12",
                "updated_at" : 1428644841,
                "names" : "DDDDDDDDDDD"
            }
        ]
    }

我正在尝试更新“rows”数组中第二个元素的“name”字段为“uid”= 14.数据由'$ set'方法更新并指定由javascript代码查找的元素位置

在shell中成功执行相同的代码。 但是在php中它无法检测到位置运算符。

1 个答案:

答案 0 :(得分:0)

如错误消息所示,您无法在数据中使用.(点)。 尝试将您的“子字段”与:分开,例如:

setObject['data:'+posU+':'+'rows'+':'+posR+':'+pKey] = pValue;