top-think / top-think/think-orm

mongo扩展pk_convert_id的问题

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

Nobody has claimed this yet.

MongoDb
Dominant language
PHP
Stars
437
Forks
188
PR merge metrics
No merged PRs in 30d

Description

mongo配置默认pk_convert_id=false
模型查询后 默认pk='id' 这样导致模型的save可能会更新多条数据
比如我有一个数据集 test 部分字段允许重复 比如title
定义了模型为Test
先查询一条数据,该条件下有多条数据,仅查第一条

$data = Test::where('title','hello')->findOrEmpty();
db.test.find({title:"hello"}).limit(1)

然后对数据进行了更新操作

$data = Test::where('title','hello')->findOrEmpty();
$data->version = 2;
$data->save();

本来以为仅更新了查询出来的那条数据,后来发现title=hello的都被修改了,看了一下sql日志发现更新条件和查询条件是一样的

db.test.update({title:"hello"},{$set:{version:2}})

后来发现默认pk_convert_id=false,模型的pk='id',更新的数据的时候会导致未找到主键所以没按主键更新条件,因此配置了pk_convert_id=true就没以上问题了

所以建议mongo扩展改进一下 不至于默认配置下出现以上错误

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

Start by tracing the Mongo extension's pk_convert_id handling and the model save/update path described in the issue. Reproduce the Test query and save with duplicate title values; done means the default configuration updates only the fetched document rather than every document matching the query.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.