[SERVER-1914] Does order of keys matter in hashes? Created: 08/Oct/10 Updated: 04/Feb/15 Resolved: 16/Jan/11 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Question | Priority: | Major - P3 |
| Reporter: | Matthew Fitzgerald | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Participants: |
| Description |
|
I have this object that has an array of hashes representing addresses. See below: > db.customers.find( {"_id":"c098443ae4243643b189fc43e071a0a3"}) , { "address1" : "101 Main Street", "city" : "Middle America", "country" : "us", "firstName" : "Matthew", "lastName" : "Fitzgerald", "phone" : "201-406-1869", "phoneExtension" : "111", "postalCode" : "55555", "slug" : "fm8x", "state" : "KS", "type" : "customer" }] } When I attempt to remove an address using "$pullAll" and unordered hash it does not remove it. Ex: This fails , ] Ex: This succeeds > db.customers.update( , ] Question: Does the order of keys in javascript hash objects matter or is it a MongoDB specific limitation? And what are the rules for ordering of keys in hashes in MongoDB (is it alphabetical by key). Is it ordered at all? Regards, |
| Comments |
| Comment by Jonathan Rodan [ 04/Feb/15 ] |
|
@eliot, correct me if I'm wrong, but if I remember well, libraries like the oficial python library can modify the order of the fields in an object. Shouldn't the order be skipped to account for those cases in which the official libraries limit the users ability to use the index? |
| Comment by Eliot Horowitz (Inactive) [ 16/Jan/11 ] |
|
BSON is ordered by design, so order does/should matter. |
| Comment by Scott Hernandez (Inactive) [ 09/Oct/10 ] |
|
Order matters and it is the order you send them to the server in. The server does not reorder them. Matches are made by basically doing a binary compare of the bson data. Each language/driver may order them differently; some languages don't have (insert) ordered map/dict impls. BTW, It isn't a hash-map but is more an array (orderd list) of pairs (string, document) as defined by the bson spec. For you specific question, you should make sure the order is the same, or use another method to remove the array values (like $elemMatch). |