Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb/PHP如果不存在,如何在嵌套数组中插入?_Php_Mongodb - Fatal编程技术网

Mongodb/PHP如果不存在,如何在嵌套数组中插入?

Mongodb/PHP如果不存在,如何在嵌套数组中插入?,php,mongodb,Php,Mongodb,我收藏的内容是这样的 { "_id" : ObjectId("50535036381ef82c08000002"),"source_references" : [ { "_id" : ObjectId("50535036381ef82c08000001"), "name" : "abc", "key" : "123" }] } 现在,如果嵌套数组中不存在名称和键,我想在的“source\u referenc

我收藏的内容是这样的

{ "_id" : ObjectId("50535036381ef82c08000002"),"source_references" : [
    {
            "_id" : ObjectId("50535036381ef82c08000001"),
            "name" : "abc",
            "key" : "123"
    }]
}
现在,如果嵌套数组中不存在名称和键,我想在的“source\u references”中插入另一个数组,否则不要插入。这是我想要的结果

{ "_id" : ObjectId("50535036381ef82c08000002"),"source_references" : [
    {
            "_id" : ObjectId("50535036381ef82c08000001"),
            "name" : "abc",
            "key" : "123"
    }
    {
            "_id" : ObjectId("50535036381ef82c08000003"),
            "name" : "reuters",
            "key" : "139215"
     }]
}
以下是我尝试过的:

$Update_tag = array('$addToSet' => array("source_references.$" => array("name" => "reuters", "key" => $r_id)));
$mycollection->update(array("_id" => $id), $Update_tag);
但我无法在嵌套数组中插入另一个数组。另外,我只想在源\u引用中插入新数组时创建“\u id”字段(嵌套数组内)


我错在哪里?希望我的问题清楚。

这很棘手,因为每个子文档都有唯一的密钥。因此,不能使用$elemMatch检查密钥/名称对是否已经存在

如果您运行的是mongodb 2.2,则可以使用聚合框架$diswind嵌套数组,然后$match用于密钥/名称对,并仅在搜索返回空时插入新元素

这是php代码:

<?php

// connect
$m = new Mongo('localhost:27017');

// select a database and collection
$db = $m->test;
$collection = $db->coll;

// sub-doc to insert if key/name pair doesn't exist
$doc = array('key'=>'12345', 'name' => 'abcde');

// aggregation command (can use $collection->aggregate for driver version 1.3.0+)
$cursor = $db->command(array('aggregate' => 'coll', 'pipeline' => array(
    array('$unwind'=>'$source_references'),
    array('$match' => array('source_references.name' => $doc['name'], 
                            'source_references.key' => $doc['key']))
)));

// if sub-doc doesn't exist, insert into main doc with this objectId
$objectId = '50535036381ef82c08000002';

if (count($cursor['result']) == 0) {
    // insert document with a new ObjectId (MongoId)
    $update_tag = array('$addToSet' => array("source_references" => array("_id" => new MongoId(), "name" => $doc['name'], "key" => $doc['key'])));
    $collection->update(array("_id" => new MongoId($objectId)), $update_tag);
} 

?>


您是否收到任何错误消息或错误结果?我没有收到任何错误消息。简单的更新不会发生@JoachimIsakssonA首先要检查两件事;您在创建数组时拼写了
soruce\u references.$
错误,并且您在预期结果中有重复的
\u id(路透社和根目录)。如果您已经假设(name+key)是唯一的,您是否需要在源引用数组中嵌入\u id字段?@JoachimIsaksson这不是问题。我手动更改了它。我想问的是,在进一步行动之前,如何在这样一个场景中插入。谢谢你的回答。请你详细说明一下你的答案,好让我能更明白一点吗?此命令是否仅适用于mongo db 2.2。。因为我没有运行mongodb-2.2。