arachide
arachide

Reputation: 8076

list/array element access in couchbase

Is it possible to store an array

var a=[
{"id":"1", "value":null},
{"id":"2", "value":null},
{"id":"3", "value":null},
{"id":"4", "value":null},
{"id":"5", "value":null}
];

in a couchbase membase with a document id

if sure, is there a way to get one element, such as

 {"id":"2", "value":null}

and add/remove one element,such as

 {"id":"6", "value":null}

?

your comment welcome

Upvotes: 2

Views: 302

Answers (2)

Paddy
Paddy

Reputation: 1195

As alternative instead of using JSON, a binary format could be used instead. Which will allow the use of the append operation. In this case for a delete you could append with the id prefixed with a minus sign.

For example the data could look like this:

id:"1",value:"null"
id:"2",value:"null"
-id:"1"

At some point you will have to grab the whole document and "clean" it up to stop it growing unbounded. If a delete operation is the unlikely case then it might be worth grabbing the whole document and deleting the id instead of appending with the minus.

Befits

  • Reduces the network bandwidth as their is no need to get the whole document
  • No need to worries about CAS or locks if the order does not matter

Costs

  • View will be harder to use
  • Have to create a custom serialiser
  • Will need to manage the document to stop it growing unbounded.

The befits only outweigh the costs when working on very large objects.

Upvotes: 0

Vitaliy
Vitaliy

Reputation: 485

Is it possible to store an array

Yes, you just need to serialize it

and add/remove one element

No, you will need to request all document by id, deserialize it's to array, remove element, serialize array and update document.

Upvotes: 1

Related Questions