[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"}

)
{ "_id" : "c098443ae4243643b189fc43e071a0a3", "addresses" : [

{ "address1" : "18 W 18th Street", "address2" : "Floor 9", "city" : "New York", "companyName" : "The OpenSky Project", "country" : "US", "firstName" : "Matthew", "lastName" : "Fitzgerald", "phone" : "201-406-1869", "postalCode" : "10016", "slug" : "2fghj2x", "state" : "NY", "subtype" : 2, "type" : "customer" }

,

{ "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
> db.customers.update(

{ "_id": "c098443ae4243643b189fc43e071a0a3" }

,
{ "$pullAll":
{ "addresses":
[

{ "slug": "fm8x", "type": "customer", "firstName": "Matthew", "lastName": "Fitzgerald", "address1": "101 Main Street", "city": "Middle America", "state": "KS", "postalCode": "55555", "country": "us", "phone": "201-406-1869", "phoneExtension": "111" }

]
}
}
)

Ex: This succeeds

> db.customers.update(

{ "_id": "c098443ae4243643b189fc43e071a0a3" }

,
{ "$pullAll":
{ "addresses":
[

{ "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" }

]
}
}
)

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,
Matt



 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).

Generated at Thu Feb 08 02:58:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.