includeArrayIndex using $unwind in always shows index as zero

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: 1.7.1
    • Component/s: Aggregation
    • None
    • None
    • Go Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Detailed steps to reproduce the problem?

      Create aggregate in compass with the following pipeline:

      bson.A{
          bson.D{
              {"$sort",
                  bson.D{
                      {"timestamp", 1},
                      {"created_at", 1},
                  },
              },
          },
          bson.D{
              {"$group",
                  bson.D{
                      {"_id",
                          bson.D{
                              {"timestamp", "$timestamp"},
                              {"storage_system_id", "$storage_system_id"},
                              {"name", "$name"},
                          },
                      },
                      {"count", bson.D{{"$sum", 1}}},
                      {"duplicates", bson.D{{"$push", "$$ROOT"}}},
                  },
              },
          },
          bson.D{{"$match", bson.D{{"count", bson.D{{"$gt", 1}}}}}},
          bson.D{
              {"$project",
                  bson.D{
                      {"duplicates", 1},
                      {"_id", 0},
                  },
              },
          },
          bson.D{
              {"$unwind",
                  bson.D{
                      {"path", "$duplicates"},
                      {"includeArrayIndex", "arrayIndex"},
                      {"preserveNullAndEmptyArrays", true},
                  },
              },
          },
          bson.D{{"$match", bson.D{{"arrayIndex", bson.D{{"$ne", 0}}}}}},
      }

      After running the pipeline, the duplicate documents are shown as aggregate output. Since the last stage $match only includes arrayIndex $ne 0, the output includes all duplicates and their non-zero Index, which are the documents I want to delete.

       

      When I execute the same in go code, the aggregate produces the same 142 documents, so the arrayIndex logic is working, however, the output of the arrayIndex in the cursor results is always 0.

      Definition of done: what must be done to consider the task complete?

      The exact Go version used, with patch level:

      $ go version

      The exact version of the Go driver used:

      $ go list -m go.mongodb.org/mongo-driver

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      Security Vulnerabilities

      If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here

            Assignee:
            Andreas Braun
            Reporter:
            Jeff Thompson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: