Skip to content

Conversation

@JamesKovacs
Copy link
Contributor

The API to create a clustered index is:

var options = new CreateCollectionOptions<Product>
{
    ClusteredIndex = new ClusteredIndexOptions<Product>
    {
        Key = Builders<Product>.IndexKeys.Ascending(x => x.Id),
        Unique = true,
        Name = "custom clustered index key name"
    }
};
db.CreateCollection("products", options);

The Key must be { _id: 1 } and Unique must be true at the moment, but the spec was written in such a way that the server can remove these requirements in the future and not require additional work by drivers. If you create a new ClusteredIndexOptions<T>(), these options will be defaulted automatically.

Ideally I wanted to support non-generic CreateCollectionsOptions.ClusteredIndex, but CreateCollectionOptions<TDocument> derives from CreateCollectionOptions. I couldn't think of a way to define a CreateIndexOptions.ClusteredIndex property with type ClusteredIndexOptions<BsonDocument> but then change the type of the property on the derived type to CreateCollectionOptions<TDocument>.ClusteredIndex to ClusteredIndexOptions<TDocument>. I opted to only support the ClusteredIndex property on CreateCollectionOptions<TDocument>. If anyone has ideas, I'm all ears.

@JamesKovacs JamesKovacs requested a review from rstam May 16, 2022 23:46
{ "key", options.ClusteredIndex.Key.Render(documentSerializer, serializerRegistry) },
{ "unique", options.ClusteredIndex.Unique },
{ "name", options.ClusteredIndex.Name, options.ClusteredIndex.Name != null }
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ClusteredIndex is being "rendered" at the wrong level here. I suggest:

var clusteredIndex = options.ClusteredIndex?.Render(documentSerializer, serializerRegistry);

This is similar to how the Validator is rendered just below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And adding a Render method to ClusteredIndexOptions:

internal BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
{
    return new BsonDocument
    {
        { "key", _key.Render(documentSerializer, serializerRegistry) },
        { "unique", _unique },
        { "name", _name, _name != null }
    };
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: lots of our Render methods are public. Perhaps this one should be also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Moved. It's a grab bag whether our Render methods are internal or public. I'll stick with internal for now.

@rstam
Copy link
Contributor

rstam commented May 18, 2022

I think adding ClusteredIndexOptions<TDocument> to CreateCollectionOptions<TDocument> is fine.

The non-generic CreateCollectionOptions only exists for backward compatibility. If we were starting from a clean slate the non-generic base class would not exist and we would only have CreateCollectionOptions<TDocument>.

@JamesKovacs JamesKovacs requested a review from rstam May 18, 2022 19:53
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JamesKovacs JamesKovacs merged commit b46bdf2 into mongodb:master May 18, 2022
DmitryLukyanov pushed a commit to DmitryLukyanov/mongo-csharp-driver that referenced this pull request May 25, 2022
DmitryLukyanov pushed a commit to DmitryLukyanov/mongo-csharp-driver that referenced this pull request May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants