Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-841

Allow AutoIndexId and Capped to be set to false as well as true in CollectionOptionsBuilder

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.9
    • Affects Version/s: 1.8.3
    • Component/s: None
    • None

      This native Mongo shell command works fine

      db.createCollection("noautoid", { autoIndexId: false })
      

      i.e. a collection created with no index on `_id`.

      But I cannot achieve the same using

      CollectionOptionsBuilder.SetAutoIndexId(false)
      

      The index is always created.

      I took a look at the code:

      MongoDB.Driver\Builders\CollectionOptionsBuilder.cs(104):

              public CollectionOptionsBuilder SetAutoIndexId(bool value)
              {
                  if (value)
                  {
                      _document["autoIndexId"] = value;
                  }
                  else
                  {
                      _document.Remove("autoIndexId");
                  }
                  return this;
              }
      

      It looks like `SetAutoIndexId(false)` makes the option removed, so that the
      default is used which is presumably `true`.

      Perhaps the correct code would be

              public CollectionOptionsBuilder SetAutoIndexId(bool value)
              {
                  if (value)
                  {
                      _document["autoIndexId"] = value;
                  }
                  return this;
              }
      

      which adds the option `

      { autoIndexId: false }

      ` explicitly, like the Mongo shell
      command does and works as desired.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            nightroman Roman Kuzmin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: