简体   繁体   中英

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, eg:

// 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM