{"action":"opened","issue":{"url":"https://api.github.com/repos/go-mgo/mgo/issues/132","labels_url":"https://api.github.com/repos/go-mgo/mgo/issues/132/labels{/name}","comments_url":"https://api.github.com/repos/go-mgo/mgo/issues/132/comments","events_url":"https://api.github.com/repos/go-mgo/mgo/issues/132/events","html_url":"https://github.com/go-mgo/mgo/issues/132","id":94916840,"number":132,"title":"Document that BSON serialisation is guaranteed to preserve struct order","user":{"login":"abligh","id":1240226,"avatar_url":"https://avatars.githubusercontent.com/u/1240226?v=3","gravatar_id":"","url":"https://api.github.com/users/abligh","html_url":"https://github.com/abligh","followers_url":"https://api.github.com/users/abligh/followers","following_url":"https://api.github.com/users/abligh/following{/other_user}","gists_url":"https://api.github.com/users/abligh/gists{/gist_id}","starred_url":"https://api.github.com/users/abligh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abligh/subscriptions","organizations_url":"https://api.github.com/users/abligh/orgs","repos_url":"https://api.github.com/users/abligh/repos","events_url":"https://api.github.com/users/abligh/events{/privacy}","received_events_url":"https://api.github.com/users/abligh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-14T10:40:07Z","updated_at":"2015-07-14T10:40:07Z","closed_at":null,"body":"I believe serialising a struct to BSON does so in the order that the `struct` fields are defined (or that it isn't). Testing appears to show that it does so today, but there is no documentation that it is *guaranteed* to do so (or that it isn't).\r\n\r\nIt would be very useful if this was a guaranteed property, for instance so that hashes of BSON values of documents could be compared (see for example: http://stackoverflow.com/questions/31371339/is-mgo-bson-marshalling-guaranteed-to-preserve-order-of-struct-components ). However, if it is not guaranteed, it would also be useful to tell people so they don't rely on it.\r\n\r\n`gob` for instance is careful to say at: https://golang.org/pkg/encoding/gob/\r\n> *Structs are sent as a sequence of (field number, field value) pairs ... The field number is defined by the type of the encoded struct: the first field of the encoded type is field 0, the second is field 1, etc. ... [The] fields are always sent in order of increasing field number*"}}