简体   繁体   English

使用php使用单个插入语句在mongodb集合中插入多个文档

[英]Insert multiple documents in mongodb collection with a single insert statement using php

In PHP I have some data to insert.在 PHP 中,我有一些数据要插入。 The sample data is shown below示例数据如下所示

Array
(
    [0] => Array
        (
            [uuid] => membership-60f929a1989d6960400020
            [channel] => channel-594b9e3568ac3969784876
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@xyz.com
        )

    [1] => Array
        (
            [uuid] => membership-60f929a198a1a549293025
            [channel] => channel-594b9edcdea14894901526
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@xyz.com
        )

    [2] => Array
        (
            [uuid] => membership-60f929a198a2b975658255
            [channel] => channel-5ab261a60fe87688298186
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@vizsafe.com
        )

    [3] => Array
        (
            [uuid] => membership-60f929a198a3b292013762
            [channel] => channel-5e668d5522526659309093
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@vizsafe.com
        )

    [4] => Array
        (
            [uuid] => membership-60f929a198a4a688473796
            [channel] => channel-5e668d6fafbbe937126299
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@vizsafe.com
        )    
   

)

I would like to insert each element of the array as single document.我想将数组的每个元素作为单个文档插入。 For example :-例如 :-

Array (
                [uuid] => membership-60f929a1989d6960400020
                [channel] => channel-594b9e3568ac3969784876
                [user] => user-5b1e6e1b636e5325325732
                [emailnotifications] => off
                [email] => demo@xyz.com
       )

should be inserted as a separate document .应作为单独的文件插入。 This should be achieved using a single insert statement .这应该使用单个插入语句来实现。 Also the version of mongodb being used is 2.4 so I cannot use insertMany.此外,正在使用的 mongodb 版本是 2.4,所以我不能使用 insertMany。 Can this be done ?这能做到吗? Any leads would be helpful.任何线索都会有所帮助。 Thanks for your response in advance.感谢您提前回复。

update更新

It's not possible for v2.4. v2.4 是不可能的。

The batch insert was added to the PHP mongo driver in v1.5.0 combined with the switch to mongodb v2.6 write protocol : PHP mongo 驱动在 v1.5.0 中加入了批量插入结合切换到mongodb v2.6 写协议

It supports all new features for MongoDB 2.6, including:它支持 MongoDB 2.6 的所有新功能,包括:

  • Aggregate can now return a cursor聚合现在可以返回一个游标
  • Aggregation pipelines can now be explained现在可以解释聚合管道
  • Possible to set maxTimeMS for commands and queries可以为命令和查询设置 maxTimeMS
  • Transparent support for the new command-based MongoDB write API对新的基于命令的 MongoDB 写入 API 的透明支持
  • New MongoWriteBatch classes (using the new MongoDB write API)新的 MongoWriteBatch 类(使用新的 MongoDB 写入 API)

Previous answer:上一个答案:

The question is about legacy driver https://pecl.php.net/package/mongo/1.5.1问题是关于遗留驱动程序https://pecl.php.net/package/mongo/1.5.1

You need to use MongoInsertBatch to insert multiple documents.您需要使用 MongoInsertBatch 插入多个文档。

From https://www.php.net/manual/en/class.mongoinsertbatch.php (doesn't exist any more):来自https://www.php.net/manual/en/class.mongoinsertbatch.php (不再存在):

$docs = array();
$docs[] = array("my" => "demo");
$docs[] = array("is" => "working");
$docs[] = array("pretty" => "well");

$batch = new MongoInsertBatch($collection);
foreach($docs as $document) {
    $batch->add($document);
}
$retval = $batch->execute(array("w" => 1));

In your case $collection is $this->db->memberships在你的情况下$collection$this->db->memberships

As a side note, you really need to upgrade the stack.作为旁注,您确实需要升级堆栈。 It's a decade old.它有十年的历史了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM