Select only necessary fields from document

Hi, is there any way to select only necessary fields from the document (without index), and decrease read operations?

I mean, I have a huge document, with a lot of fields, some fields are very large (base64 images).

{
  images: ['first', 'second', 'third'],
  a: 'foo',
  b: 'bar',
}

For example, I need to get only the first image + ‘a’ field.

I’ve already posted a question about indexing the first item on an array here, but it does not work for me, because my images are larger than 64kb.

If I use Select, then, if I understand it in the right way, still all document data are read and consume read operations.

Now I duplicate data and use different collections.

You might want to model your data differently.

Instead of including base64 images in your documents that, presumably, describe something to do with the images, store those images as separate documents.

Then, in your current documents, replace the image data with references to the images that should exist.

For example:

{
  images:[Ref(Collection("images"), "1"), Ref(Collection("images"), "2"), ... ],
  a: 'foo',
  b: 'bar',
}

Then, reading the documents that contain only references to images involves reading far less data:

Let(
  {
    doc: Get(Ref("documents", "1234")),
    first_image: Select(["data", "images", 0], Var("doc")),
    field_a: Select(["data", "a"], Var("doc")),
  },
  {
    first_image: Get(Var("first_image")),
    a: Var("field_a")
  }
})
1 Like

You can also use bindings to add a computed field to an index, if you really don’t want to split into another Collection as Ewan proposed.

@ridenaio Do you still have any questions on this, or do you have enough information to mark a post as a solution? I would be interested in learning more about how you decided to move forward with your data model.

Cheers!

Well, but fields are too big (larger than 64kb), so if I use binding, it does not matter? There are no limits on index with bindings?