Find Documents when structure changes

Hi,

I’m fairly new to NoSql and Fauna so apologies if this is a stupid question.

If I have a collection of documents and then at some point I add a new field like this say.
Originally documents would be like this:
{ “fieldA”:“test1” }
In the future documents now look like this:
{ “fieldA”:“test2”, “field2”: “true” }

If I create an index on field2 then that would exclude all the original documents.

How would I be able to search for all documents that have “field2” = true or no “field2” at all?

Thanks

Hello,

As you’ve probably seen, it’s not possible to match a null value on an index. But this is where index bindings shine.

To make use of them in this case, you’ll first create an index using a binding where field2 is null, then perform a Union on that index and the one created for field2 being true (this is using a collection called structure_change, you’ll want to update the source to whatever your actual collection is called):

CreateIndex({
  name: "after_change",
  unique: false,
  serialized: true,
  source: Collection("structure_change"),
  terms: [
    {
      field: ["data", "field2"]
    }
  ]
})

CreateIndex({
  name: "sc_by_null_field2",
  source: [{
    collection: Collection("structure_change"),
    fields: {
      null_field2: Query(
        Lambda(
          "doc",
          Equals(Select(["data", "field2"], Var("doc"), null), null)
        )
      )
    }
  }],
  terms: [ {binding: "null_field2"} ],
})

So now when you run a Union you’ll get both documents back:

Map(
  Paginate(
    Union(
      Match(Index("after_change"), true),
      Match(Index("sc_by_null_field2"),true)
    )
  ),
  Lambda("doc", Get(Var("doc")))
)

{
  data: [
    {
      ref: Ref(Collection("structure_change"), "300846027025416713"),
      ts: 1623167979290000,
      data: {
        fieldA: "test"
      }
    },
    {
      ref: Ref(Collection("structure_change"), "300846100224410121"),
      ts: 1623168049112000,
      data: {
        fieldA: "test2",
        field2: true
      }
    }
  ]
}

Thanks very much @Cory_Fauna for your reply and code. That explanation was very helpful.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.