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条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程