Search code examples
faunadb

Index return "data" field entirely in fauna db


I am trying to create an index that returns entire data object of the documents in a collection; here is the code:

CreateIndex({
  name: "users_by_data",
  source: Collection("users"),
  values: { field: ['data'] }
})

but after creation it says: Values Not set (using ref by default) enter image description here

If I specifically define fields (separately by their name), it will behave as expected, but data isn't working. the question is:


Is it impossible (e.g. for performance reasons) or am I doing it wrong?

side note: I am aware that I can do Lambda function on Paginate and achieve similar result, but this question specifically is about the Index level;


Solution

  • You can currently index regular values (strings, numbers, dates, etc) and you can index an array which will more or less 'unroll' the array into separate index entries. However, what you are trying, indexing an object is not possible at this point. An object (like data) will be ignored if you try to index it.

    Currently, you have two options:

    • as you mentioned, using Map/Get at query time.
    • listing all values of the data object in the index since you can select specific values of an object in the index (which is however less flexible if new attributes arrive later on in the object)

    We intend to support indexing of objects in the future, I can't provide an ETA yet though. There is a feature request on our forums as well that you can vote up: https://forums.fauna.com/t/object-as-terms-instead-of-scalar-s/628