Atomic mutateIn remove operations

Assuming I have a couchbase document:

{
 "a": 1,
 "c": 2
}

What I want to do is to remove paths “a” and “b”. (I do not know if these fields are exists)

val mutateInSpecs = Seq(
 MutateInSpec.remove("a"),
 MutateInSpec.remove("b"),
)

collection.mutateIn(docId, mutateInSpecs)

Because of the atomicity feature of mutateIn operations. I get path not found exception for key “b” and key “a” is not removed.
Of course, I can do get query to see which fields are exists and update my mutateInSpecs.

However, Is there an option like removeIfPathExist which lets me remove fields that are only exist and ignore the ones that are not found?

I don’t find any such option. MutateInOptions (Couchbase Java SDK 3.4.11 API)

If the goal is to delete all, I wonder if a wild card in the path would work.

1 Like

Hi @burak.dursunlar
No there’s no such conditional mutateIn operator.
But here’s a little trick you can use:

val mutateInSpecs = Seq(
 MutateInSpec.upsert("a", ""),
 MutateInSpec.upsert("b", ""),
 MutateInSpec.remove("a"),
 MutateInSpec.remove("b")
)

If that makes sense? E.g. by always upserting the fields first, you guarantee that they’re present when the removes run.

2 Likes

Wow, very nice! Should have come up with that idea. Thanks :pray:

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