mongo_client批量插入的时候部分_id重复无法获取实际插入的数量

Open
#52 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
mongodb, php
Domain
databases

Research direction

The entry point shown is $mongoTable->insertMany(..., ['ordered' => false]); start by tracing how its bulk-write result and ServiceException are propagated. Reproduce the duplicate _id case and verify that callers can obtain the actual count of non-duplicate inserts while retaining the duplicate-key error.

Written by the indexing model from the issue text.

Description

伪代码:

<?php
$insertMany = [
  { _id: 1, qty: 20 },
  { _id: 2, qty: 55 },
  { _id: 2, qty: 30 },
  { _id: 3, qty: 100},
  { _id: 3, qty: 120},
  { _id: 4, qty: 20},
  { _id: 5, qty: 30}
];
try {
  $insertResult = $mongoTable->insertMany($insertMany, ['ordered' => false]);
  printf("Inserted %d document(s)\n", count($insertResult->getInsertedIDs()));
} catch (ServiceException $e) {
  print_r($e->getMessage());
  //output like: error 'error while executing mongo command: bulk write error: [{[{E11000 duplicate key error collection: db_aaa.t_table index: _id_ dup key: { _id: "2"}} {E11000 duplicate key error collection: db_aaa.t_table index: _id_ dup key: { _id: "3" }}]}, {<nil>}]' on 'Hyperf\GoTask\Relay\CoroutineSocketRelay'
} catch (GoridgeException $e) {
  //other logic
}

ordered是false的时候,会继续插入非重复的数据,但是ServiceException没有提供实际写入的数据,可靠性不好控制。

Dominant language
PHP
Stars
255
Forks
29
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from hyperf/gotask

All issues in hyperf/gotask

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.