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

Weird behaviour with the `$merge` stage of an aggregation

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 5.3.1
    • Component/s: Aggregation
    • None
    • None
    • Java Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      consider two collections  collectionA and collectionB. When we do this aggregation

      // code placeholder
      collectionA.aggregate(…)
      

      with a pipeline like

      [
      {
         $match: {foo: "bar"}
      },
      {
        $merge: {
          "into": "collectionB",
          "on": "_id",
          "whenMatched": "merge",
          "whenNotMatched" : "fail"
      } 
      ]
      

      Then the business code is returned a cursor that iterates over the whole collectionB.  

      From my experiments, it looks as if the command executed by the java driver is equivalent to:

      db.collectionA.aggregate(…) // wait until the command is over
      db.collectionB.find({}) // return this cursor to the caller.
      

       

      The cursor returned by the aggregation is supposed to yield no document, by design of the $merge stage. Somehow, the cursor returned yield the content of the whole collection that is the target of the $merge.

      • It does not matter if the $match matches something or not.
      • In the case there is a match, then the merge is observed in collectionB : the write does occur.
      • this is not reproducible in the mongo shell or with the golang driver
      • this is reproducible with a bunch of mongodb server including
        • mongodb-community/6.0.4
        • atlas cluster 8.0.4
        • docker image mongodb/mongodb-atlas-local:8.0.4

            Assignee:
            valentin.kovalenko@mongodb.com Valentin Kavalenka
            Reporter:
            frederic.dejaeger@sonos.com Frédéric De Jaeger
            None
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: