Hello everyone, I’m basically trying to access a map within a document and increase a value by 1 in it, here’s my code
public void incrementInMap(UUID uuid, String map, String key) {
Document document = new Document("UUID", uuid.toString());
mongoCollection.find(document).first((foundDocument, throwable) -> {
System.out.println(foundDocument);
if (foundDocument == null) {
Document mapDocument = new Document();
mapDocument.put(key, 1);
document.put(map, mapDocument);
mongoCollection.insertOne(document, ($, throwable2) -> {});
return;
}
if (foundDocument.get(map) == null) {
System.out.println("Focking map is null");
Document mapDocument = new Document();
mapDocument.put(key, 1);
mongoCollection.updateOne(foundDocument, Updates.set(map, mapDocument), ($, throwable3) -> {});
return;
}
if (!foundDocument.get(map, Document.class).containsKey(key)) {
System.out.println("Focking value null");
Document mapDocument = foundDocument.get(map, Document.class);
mapDocument.put(key, 1);
mongoCollection.updateOne(foundDocument, Updates.set(map, mapDocument), ($, throwable4) -> {});
return;
}
Document mapDocument = foundDocument.get(map, Document.class);
System.out.println("Map Document -> " + mapDocument);
mapDocument.replace(key, mapDocument.getInteger(key) + 1);
System.out.println("New Map Document ->" + mapDocument);
mongoCollection.updateOne(foundDocument, Updates.set(map, mapDocument), ($, throwable5) -> {});
});
}
Although mongo seems to be ignoring the last line particularly, I tried doing the same code with an integer or a string it does change it but for some reason doing it with a map does not work, Any ideas on fixing this?