[SERVER-2592] The fields in a document are reordered (sorted alphabetically) when setting a field value Created: 19/Feb/11 Updated: 01/May/23 Resolved: 14/Aug/13 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Write Ops |
| Affects Version/s: | 1.8.0-rc0 |
| Fix Version/s: | 2.5.2 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Sridhar Nanjundeswaran | Assignee: | Andrew Morrow (Inactive) |
| Resolution: | Done | Votes: | 7 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
OS X. 1.8.0_rc0 |
||
| Issue Links: |
|
||||||||||||||||||||||||||||||||
| Operating System: | ALL | ||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||
| Description |
|
The fields in a document are reordered when the size of the value stored in it changes (increases or decreases) Case 1: No change in size of field, so no change in field order ) db.testcol.update( {a:1},{$set:{c:22}}) Case 2: Field size changes and the fields are reodered ) db.testcol.update( {a:1},{$set:{c:"foobar"}}) Case 3: Field size changes and the fields are reordered. Note : the _id field is moved to the last field on the node ) > db.testcol.update( {A:1},{$set:{C:"f"}}) |
| Comments |
| Comment by Dwight Merriman [ 02/Nov/12 ] | |||||||||
|
i'd vote for maintaining order on updates, i find it nonintuitive and confusing when things move around.
| |||||||||
| Comment by Arun Bhalla [ 24/Apr/12 ] | |||||||||
|
More motivation for why this can be an issue: | |||||||||
| Comment by Scott Hernandez (Inactive) [ 19/Jun/11 ] | |||||||||
|
It is just the fields at the level you are modifying. The _id field is not reordered like other embedded fields. > db.s2592.insert({_id:{b:1, a:1}}) } , "v" : 1 } | |||||||||
| Comment by Thilo Planz [ 19/Jun/11 ] | |||||||||
|
Does this also happen to embedded documents when they themselves are not updated (i.e. just other parts of the document are modified)? Also, are there situations when this can happen even when the document is never updated (something like rebuilding databases, for example). I am asking because I am using embedded documents as _id fields and range queries against those (as I was advised on the mailing list that this would work), so field reordering on the _id document would be really bad. I am, however, never updating the _id field itself (and usually not even the document at all). So can I still hope to not be affected by this issue? | |||||||||
| Comment by Stephane Jouanneau [ 09/Apr/11 ] | |||||||||
|
// My problem is the same... // First create a record // Correct // Then add a field with "$set" // Bad : all fields are resorted... // I hope an option will be available soon for "$set" such as "end","begin" or "after" such as : | |||||||||
| Comment by Scott Hernandez (Inactive) [ 19/Feb/11 ] | |||||||||
|
I've updated the docs: http://www.mongodb.org/display/DOCS/Updating#Updating-Field%28re%29order | |||||||||
| Comment by Scott Hernandez (Inactive) [ 19/Feb/11 ] | |||||||||
|
Here is the example when a unique index exists on an embedded document and a re-order breaks things. > db.test1.ensureIndex( {a:1}, {unique:true}) | |||||||||
| Comment by Scott Hernandez (Inactive) [ 19/Feb/11 ] | |||||||||
|
Yes, when range queries are applied to an embedded doc for one. > db.test1.save({_id: {b:1, a:2}, b:2, c:3, a:{b:1, a:2}} }}) , "b" : 2, "c" : 3, "a" : { "b" : 1, "a" : 2 } } }}) , , | |||||||||
| Comment by Eliot Horowitz (Inactive) [ 19/Feb/11 ] | |||||||||
|
This is intentional. |