[SERVER-53895] updateOne and pull cannot remove content other than the first element Created: 20/Jan/21 Updated: 27/Oct/23 Resolved: 20/Jan/21 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Usability |
| Affects Version/s: | 4.4.3 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | qingzhi li | Assignee: | Edwin Zhou |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Operating System: | ALL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Steps To Reproduce: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Participants: |
| Description |
|
I am a normal user of mongo. When I use mongoDB to perform a combination of updateOne and pull, its behavior does not meet my expectations. I am not sure if it is a bug or the result of following established rules. The official document's description of updateOne is that it will update a single document, but in my test statement, if the selector can hit multiple records, mongo will only check whether the first record meets the requirements, and if it does not meet the requirements, it will not check the following Record whether the requirements are met.
As shown in the steps to reproduce, my question is why do the same operations on id 1 and 2 return different results? From what I have observed, the explanation I can give is because id 1 is in the first position, and updateOne will only check this one. Is this correct? |
| Comments |
| Comment by qingzhi li [ 21/Jan/21 ] | |||||
|
Thank you for your reply. Now I know it. | |||||
| Comment by Edwin Zhou [ 20/Jan/21 ] | |||||
|
db.collection.updateOne() will select and modify only one document. A small correction in your guess:
The pull operation doesn't query for documents, but rather it will update the documents that have been queried. Since updateOne({}) updates only one document and the query is empty, it selects only
and tries to pull { _id: 5 } from scores. Since scores does not contain an element with { _id: 5 }, nothing will change. You can update multiple documents using db.collection.update() with { multi: true }
which will pull { _id: 5 } from all documents with the field scores containing { _id: 5 }. Best, | |||||
| Comment by qingzhi li [ 20/Jan/21 ] | |||||
|
My guess is that because the selector is empty, the pull operation will only look for the first record, so this is a feature and not a bug. If this is the case, please close this one jira, thank you. |