You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/11/09 15:14:51 UTC

[GitHub] [nifi] tpalfy commented on a change in pull request #5482: NIFI-9334 Add support for upsert in 'PutMongoRecord'.

tpalfy commented on a change in pull request #5482:
URL: https://github.com/apache/nifi/pull/5482#discussion_r745715413



##########
File path: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongoRecord.java
##########
@@ -190,4 +303,49 @@ private List convertArrays(Object[] input) {
 
         return retVal;
     }
+
+    private Bson[] buildFilters(Map<String, List<String>> updateKeyFieldPathToFieldChain, Document readyToUpsert) {
+        Bson[] filters = updateKeyFieldPathToFieldChain.entrySet()
+            .stream()
+            .map(updateKeyFieldPath__fieldChain -> {
+                String fieldPath = updateKeyFieldPath__fieldChain.getKey();
+                List<String> fieldChain = updateKeyFieldPath__fieldChain.getValue();
+
+                Object value = readyToUpsert;
+                String previousField = null;
+                for (String field : fieldChain) {
+                    if (!(value instanceof Map)) {
+                        throw new ProcessException("field '" + previousField + "' (from field expression '" + fieldPath + "') is not an embedded document");
+                    }
+
+                    value = ((Map) value).get(field);
+
+                    if (value == null) {
+                        throw new ProcessException("field '" + field + "' (from field expression '" + fieldPath + "') has no value");
+                    }
+
+                    previousField = field;
+                }
+
+                Bson filter = Filters.eq(fieldPath, value);
+                return filter;
+            })
+            .collect(Collectors.toList())
+            .toArray(new Bson[0]);
+
+        return filters;
+    }
+
+    private boolean updateModeIs(String updateValueToMatch, ProcessContext context, FlowFile flowFile) {

Review comment:
       This method is currently called like this:
   ```java
   if (updateModeIs(UPDATE_ONE.getValue(), context, flowFile)) {
   ...
   } else if (updateModeIs(UPDATE_MANY.getValue(), context, flowFile)) {
   ```
   
   With your suggestion it would look like this:
   ```java
   if (updateModeToMatch(UPDATE_ONE.getValue(), context, flowFile)) {
   ...
   } else if (updateModeToMatch(UPDATE_MANY.getValue(), context, flowFile)) {
   ```
   
   I think the current one is better.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org