bucket…mutateIn(id).upsert(“dict.nestedDict”, getMap(), new SubdocOptionsBuilder().createPath(true)).execute();
But I get below. Just curious whether is there is way to preserve subdocuments that are not matching with the map object. Basically only the map object should
be inserted or updated based on on the map key.
Hi @y2k1975
That code snippet will replace the contents of dict.nestedDict with the contents of getMap(), which is what you’re seeing.
So you want a way to merge a map with an existing one? There’s not a built-in Sub-Document spec for that, but if the new map has a small number of keys, you could loop over it and build it up manually? Like this:
JsonObject newMap;
if (newMap.getNames().size() > 16) {
throw new RuntimeException("Too many fields!");
}
List<MutateInSpec> specs = new ArrayList<>();
for (String key : newMap.getNames()) {
specs.add(MutateInSpec.upsert("dict.nestedDict." + key, newMap.getLong(key)).createPath());
}
collection.mutateIn(id, specs);
(nb I’ve written this example in SDK3 rather than SDK2, as SDK2 is only about 6 months off EOL at this point. But you can easily adapt the example for SDK2 of course.)
Of course, this does have the limitation that you can only support 16 fields in the map. If you have more then you could use @vsr1’s suggestion above, or do a full-document get-and-replace, or do multiple mutateIn calls.
Thank you @graham.pople. At this point we are using 2.7 and I will check the same with this version. At this point we are having less than 16 fields and no possibility to go beyond that in near future. I will try with equivalent way to achieve using 2.7 version. Also @vsr1 is there is anything specific we need to build in index if we are using those queries.