Why are the order changed when updating a document?

$query = CouchbaseN1qlQuery::fromString(“UPDATE bucket SET active = ‘no’”);
$result = $bucket->query($query);

Before I updated the document it looked like this:

myfield": “no”,
“active”: “yes”

After update of active from yes to no it looks like this:

“active”: “no”,
myfield": “no”

Why are couchbase changing the order in the document?

The important thing here is that JSON objects are inherently un-ordered. From RFC 7159:

An object is an unordered collection of zero or more name/value
pairs, where a name is a string and a value is a string, number,
boolean, null, object, or array.

An array is an ordered sequence of zero or more values.

As the fields in the object are keyed, it shouldn’t matter what order they’re in - my_doc['my_field'] is always going to evaluate to that field regardless of the apparent ordering. If ordering is important within a field, then it sounds like an array is more what you’re looking for.

Couchbase usually returns documents with the field names in alphabetical order. I’m most familiar with N1QL queries, where this ordering is the result of the JSON marshalling/unmarshalling library involved in processing the documents, but it also has a positive effect in making it easier to find fields when looking through the result set.

And as @JFlath notes, the order doesn’t change the meaning.

Ok, thanks JFlath and eben for clarifying that.