Hi @nagarajan_subbaraj !
It is indeed possible to upsert both the document content and an XATTR at the same time.
For the document content, the server wants you to use MutateInSpec.replace
with an empty path.
SIDEBAR: Why not MutateInSpec.upsert
? Your guess is as good as mine!
Another thing to note is that when doing mutateIn
on a document that might not exist, it’s necessary to specify StoreSemantics.UPSERT
.
Here’s a code snippet that puts it all together:
public static MutationResult upsertWithXattr(
Collection collection,
String documentId,
Object documentValue,
String xattrPath,
Object xattrValue
) {
return collection.mutateIn(
documentId,
List.of(
MutateInSpec.replace("", documentValue),
MutateInSpec.upsert(xattrPath, xattrValue).xattr().createPath()
),
MutateInOptions.mutateInOptions()
.storeSemantics(StoreSemantics.UPSERT)
);
}
Call it like this:
upsertWithXattr(
collection,
documentId,
Map.of("foo", "bar"),
"myAttrs.magicWord",
"xyzzy"
);
Then if you want, you can use lookupIn
to get both the document content and XATTR at the same time, like this:
LookupInResult result = collection.lookupIn(documentId, List.of(
LookupInSpec.get(""),
LookupInSpec.get("myAttrs").xattr()
));
System.out.println("Content: " + new String(result.contentAsBytes(0)));
System.out.println("XATTRs: " + new String(result.contentAsBytes(1)));
which prints:
Content: {"foo":"bar"}
XATTRs: {"magicWord":"xyzzy"}
Thanks,
David