Update function results in no database update within an upsert and index context

I have the following FQL upsert function:

      self.client.query(
          q.map_(
              lambda x: q.if_(
                  q.exists(
                      q.match(
                          q.index(index),
                          q.select(term1, x),
                          q.select(term2, x),
                          q.select(term3, x)
                      )
                  ),
                  q.update(q.collection(collection), {'data': x}),
                  q.create(q.collection(collection), {'data': q.merge(x, insert)})
              ),
              update
          )
      ))

Surprisingly, where it fails is in the update part when a document already exists.
The document does not get updated as one would expect with the Update function (which has a very straightforward and logical intended action).
I have included the entire upsert function above in case that my problem may have something to do with the context of running an update, or perhaps the index structure which has three terms and no values (i.e. all values are returned in the case of a match).

Example:
existing document = {‘term1’: term1, ‘term2’: term2, ‘term3’: term3, ‘tobeupdated’: beforeupdate}
update object = {‘term1’: term1, ‘term2’: term2, ‘term3’: term3, ‘tobeupdated’: afterupdate}
updated document = existing document

Confusingly the response object I get also looks quite non-sensical with mixed up fields from various documents as well as some older fields which were deleted a few days ago.
But I do not see anything unusual happening to the data itself, it is only the response that is scrambled. Except of course that it doesn’t update.

Can anyone see what is wrong here? Thank you.

I think the issue is that in the Update function you are passing a Collection reference and not a reference to a specific document to update.

1 Like

I concur with @wallslide.

Collections are just another kind of Document, and as such they can have a data field, too. It looks like you are actually updating the Collection itself with the data meant for the specific Document. If this is happening with many different documents, then it makes sense that the Collection’s data would be a mish-mash of those different Documents. You can check the Collection data directly with Get

self.client.query(q.get(q.collection(collection_name)))

A way to keep the match around is to use the Let function and save the value for later.

self.client.query(
  q.map_(
    lambda x: q.let(
      {
        match: q.match(
          q.index(index),
          q.select(term1, x),
          q.select(term2, x),
          q.select(term3, x),
        ),
        ref: q.select(
          ["data", 0], 
          q.paginate(q.var("match"), size=1), 
          None,
        )
      },
      q.if_(
          q.is_null(q.var("ref")),
          q.create(q.collection(collection), {'data': q.merge(x, insert)}),
          q.update(q.var("ref"), {'data': x}),
      )
    )
  )
)
1 Like

Thanks for pointing this out - I somehow overlooked that I was actually updating the collection .

Nice idea, I will study if the upsert function works out more efficiently this way.

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