trollkotze
trollkotze

Reputation: 1139

Is order preserved in Arrays/ListValues in Google Cloud Datastore?

When I store an entity in Google Cloud Datastore with some property of type array, and I retrieve it later, it seems the order of values inside the array property is always preserved in the way it was saved, e.g.:

// just some key where we save and retrieve our test entity
let key = datastore.key({
  namespace: 'whatever',
  path: ['ArrayTest', 'something']
});

// save an array of numbers in order
let numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
await datastore.save({key, data: {numbers}}); 

// ...

// retrieve that entity later:
let entity = await datastore.get(key);

console.log(entity.numbers);
/* prints out:
 * [ 'zero',
 * 'one',
 * 'two',
 * 'three',
 * 'four',
 * 'five',
 * 'six',
 * 'seven',
 * 'eight',
 * 'nine',
 * 'ten' ]
 */

I have only done small tests like this, with up to a few hundred entities and simply save and get. So far I have not seen a case where the retrieved array is ordered differently from how I saved it. But I am not sure if this is guaranteed. I could not find a statement in the docs stating explicitly that order of arrays / list values is preserved in the way they are saved.

I would like to rely on the assumption that order is preserved in that way.

Can someone confirm or reject this assumption?

Upvotes: 1

Views: 393

Answers (1)

Joshua Melcon
Joshua Melcon

Reputation: 249

Here are the docs for ArrayValue

"The order of this array may not be preserved if it contains a mix of indexed and unindexed values."

This means that value order is preserved -- if you find a case where it isn't file a bug.

Upvotes: 2

Related Questions