douzhen5158 2014-01-02 13:14
浏览 59
已采纳

如何将两个不同的文档插入到相同的mongo db集合中?

How can insert two different documents into same collection in mongo db using php? I tried the code below but it only inserted document1.

$NewCollection->batchInsert(
                array($document1, $document2),
                array('continueOnError' => true)
             );
  • 写回答

2条回答 默认 最新

  • dsuvs66406 2014-01-02 21:01
    关注

    I can't reproduce this using the following script:

    $m = new MongoClient();
    $m->test->foo->drop();
    $rs = $m->test->foo->batchInsert(
        [
            (object) ['_id' => 1],
            (object) ['_id' => 2],
        ],
        ['continueOnError' => true]
    );
    
    echo "batchInsert returned:
    ";
    
    var_dump($rs);
    
    echo "
    test.foo contents:
    ";
    
    var_dump(iterator_to_array($m->test->foo->find()));
    

    This reports a successful GLE response (ok is 1 in the $rs variable) and ultimately prints both documents that were just inserted into the collection. Playing around with the documents themselves and continueOnError behaved as expected. For instance, continueOnError as false with the following batch:

        [
            (object) ['_id' => 1],
            (object) ['_id' => 1],
            (object) ['_id' => 2],
        ]
    

    ...will yield an exception processing the second document. The third document never gets inserted. With continueOnError as true, we still get the exception but the third document does get inserted.

    One additional thought would be to attempt debugging with the stream context loggers. Modify the above script by adding the following to the MongoClient:

    function log_batchinsert($server, $docs, $options, $info) {
        var_dump(func_get_args());
    }
    
    $ctx = stream_context_create([
        'mongodb' => ['log_batchinsert' => 'log_batchinsert'],
    ]);
    
    $m = new MongoClient(null, [], ['context' => $ctx]);
    
    $m->test->foo->drop();
    $rs = $m->test->foo->batchInsert(/* ... */);
    

    This will yield some additional debug output from the driver when the batchInsert operation is sent across the wire (your results may differ slightly):

    array(4) {
      [0]=>
      array(5) {
        ["hash"]=>
        string(25) "localhost:27017;-;.;10494"
        ["type"]=>
        int(1)
        ["max_bson_size"]=>
        int(16777216)
        ["max_message_size"]=>
        int(48000000)
        ["request_id"]=>
        int(1276292850)
      }
      [1]=>
      array(2) {
        [0]=>
        object(stdClass)#4 (1) {
          ["_id"]=>
          int(1)
        }
        [1]=>
        object(stdClass)#5 (1) {
          ["_id"]=>
          int(2)
        }
      }
      [2]=>
      array(1) {
        ["flags"]=>
        int(1)
      }
      [3]=>
      array(1) {
        ["continueOnError"]=>
        bool(true)
      }
    }
    

    Here, the function logs some information about the server connection used, the batch array itself (notice that it picks up we're using stdClass), and the flags for the OP_INSERT operation. The second argument (i.e. the batch) will be your best tool (short of monitoring network traffic) for seeing exactly what the driver sent over to MongoDB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法