Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2690

Write operation using positional operator return incorrect value

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.0
    • Component/s: Write Operations
    • Labels:
      None
    • Environment:
      Ubuntu Linux 16.04, MongoDB 3.6.0-RC8, MongoDB Java Driver 3.6.0-RC1

      Hello, I'm using the new positional operator for arrays included in MongoDB 3.6 but I've come to a bug which I don't know how to overcome. I'm executing the following operation both in Mongo Shell and with the Java driver:

      db.getCollection('cloudMonitoringResource').update({"_id" : "0"}, {$push: {"deviceMetrics.$[i].metricValues.$[j].values": {"date": ISODate("2017-11-25 13:08:21.659Z"), "value": 1}}},  {arrayFilters: [{"i.metric" : "load"},{"j.day":"2017-11-25"}]})
      

      in which I get the following result:

      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
      

      Which means there's a match (my document exists with that Id) but the document was not modified since there isn't any element in the arrays matching the arrayFilters, however if I execute the update

      db.getCollection('cloudMonitoringResource').update({"_id" : "0"}, {$push: {"deviceMetrics.$[i].metricValues.$[j].values": {"date": ISODate("2017-11-25 13:08:21.659Z"), "value": 1}}},  {arrayFilters: [{"i.metric" : "availability"},{"j.day":"2017-12-03"}]})
      

      I get the following result:

      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      

      Which means 1 match, 0 upsertions but 1 document modified.

      Now running the same sequence on the Java driver, I get the same result:

      WriteResult{n=1, updateOfExisting=true, upsertedId=null}
      

      Debugging the code, I've seen this in BaseWriteOperation:

      private int getCount(final BulkWriteResult bulkWriteResult) {
              int count = 0;
              if (getType() == UPDATE || getType() == REPLACE) {
                  count = bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size();
              } else if (getType() == DELETE) {
                  count = bulkWriteResult.getDeletedCount();
              }
              return count;
          }
      

      So for an update, the n just take into account the matched count (1 in both cases) and the upserts (0 in both cases) but it ignores modifications (0 in the first case, 1 in the second) as a result, there's no way to distinguish when such an operation actually modifies the document or not so there's no way to know if the update has been successful or if the document continues unmodified.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            getaceres Jose Antonio Sanchez Murillo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: