Offset+limit for nested documents in couchbase lite


#1

Hello, I’m just starting with couchbase and have a usage question:
consider the tutorial here where there are people and their children stored in a nested array. In my use case, I have just a handful of people, but all of them have many children (let’s say up to 100k). Each child is a simple JSON, with an id and some more params, but not much more nesting.

the hierarchy, simply put in code is like this:

let mutableDocument = findOrCreateParentDocument(parentId: parentId)

for child in childrenDictionary {
  let childObject = MutableDictionaryObject(data: child)
  mutableDocument
    .array(forKey: "children")!.addDictionary(childObject)
}

I have two questions: (1) Is the model where each person has a nested array of this size a good way to model such data (I’m a little worried about perf).
(2) the children will be gradually added to the DB in small batches and I later need to query the children to display them, and will also be doing so in batches (say 400 children). In queries, is there any way to use offset and limit on the nested children (to look at just a part of the entire children array)? Or would it be better to store each child as a separate document and some use joins? Thanks in advance!


#2

That’s not a good design. You’ll end up with huge documents, which will lead to performance and memory problems because (a) a document is loaded into memory all at once, and (b) a document is currently sent in its entirety by the replicator even if only a small bit changed.

Instead you should store the children as separate documents, each with a property pointing to the parent.