php mongobulk,PHP MongoDBDriverBulkWrite 用法 手册 | 示例代码

这段代码展示了如何使用 MongoDB 的批量写入操作执行删除、插入、更新和更新操作。它创建了一个 ordered 的 bulk 写入实例,并在 MongoDB 中执行。在尝试执行时,代码捕获可能的 BulkWriteException 和其他异常,处理写入关心的问题,如未能满足的写入关注或未完成的写入操作。最后,输出插入和更新的文档数量。
摘要由CSDN通过智能技术生成

$bulk= newMongoDBDriverBulkWrite(['ordered'=>true]);$bulk->delete([]);$bulk->insert(['_id'=>1]);$bulk->insert(['_id'=>2]);$bulk->insert(['_id'=>3,'hello'=>'world']);$bulk->update(['_id'=>3], ['$set'=> ['hello'=>'earth']]);$bulk->insert(['_id'=>4,'hello'=>'pluto']);$bulk->update(['_id'=>4], ['$set'=> ['hello'=>'moon']]);$bulk->insert(['_id'=>3]);$bulk->insert(['_id'=>4]);$bulk->insert(['_id'=>5]);$manager= newMongoDBDriverManager('mongodb://localhost:27017');$writeConcern= newMongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY,1000);

try {$result=$manager->executeBulkWrite('db.collection',$bulk,$writeConcern);

} catch (MongoDBDriverExceptionBulkWriteException $e) {$result=$e->getWriteResult();// Check if the write concern could not be fulfilledif ($writeConcernError=$result->getWriteConcernError()) {printf("%s (%d): %sn",$writeConcernError->getMessage(),$writeConcernError->getCode(),var_export($writeConcernError->getInfo(),true)

);

}// Check if any write operations did not complete at allforeach ($result->getWriteErrors() as$writeError) {printf("Operation#%d: %s (%d)n",$writeError->getIndex(),$writeError->getMessage(),$writeError->getCode()

);

}

} catch (MongoDBDriverExceptionException $e) {printf("Other error: %sn",$e->getMessage());

exit;

}printf("Inserted %d document(s)n",$result->getInsertedCount());printf("Updated  %d document(s)n",$result->getModifiedCount());?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值