Efficient way for updating a nested array in a document

Hi,

I’m implementing a sensor logging framework and need to save high frequency accelerometer data.
My document is basically structured like this:

{
"name":"sensorlog01",
"data":[
         {"timestamp":4332, "value":"123"},
         {"timestamp":4333, "value":"122"}
       ]
}

Assuming we already have 10.000 measurements in this structure, is it possible to efficiently update the array appending some new values, as you would do when writing to a file or adding new rows to a relational database table?

In Swift, currently I need to copy the array around in memory. Is there a more efficient way than this:

 var newValues = newRev.properties["data"] as [[String:AnyObject]]
 newValues.append(["timestamp":123, "value":567])
 newRev["data"] = newValues

How does Couchbase Lite handle those updates, is the whole array rewritten to the database?

Thanks for advices

It is best to put each batch of sensor readings into it’s own document. In general it is bad to append to a document, as it might grow forever. Instead it is better to have lots of documents, and use a view to sort them.

If every reading is it’s own document, that could be too many, but buffering the readings in memory and then writing them out to a document ~ once per second, is probably the easiest on the hardware, and easy enough to work with.

1 Like