[CSHARP-2381] How to create unique index using IMongoIndexManager.CreateOne with CreateIndexModel Created: 09/Sep/18  Updated: 27/Oct/23  Resolved: 03/Jan/19

Status: Closed
Project: C# Driver
Component/s: Command Operations
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Matteo Contrini Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2020-10-08-16-05-00-151.png    
Issue Links:
Related
related to CSHARP-2475 Update documentation to refer to the ... Backlog

 Description   

I'm not sure when this has changed, but since the

IMongoIndexManager.CreateOne(IndexKeysDefinition<TDocument> keys, CreateIndexOptions options = null, CancellationToken cancellationToken = default(CancellationToken))

overload has been marked as deprecated, I can't find a way to create a single unique index without using deprecated methods.

In fact, the suggested one only accepts a CreateOneIndexOptions, which doesn't contain a property for setting the index as unique.

IMongoIndexManager.CreateOne(CreateIndexModel<TDocument> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default(CancellationToken));

Plus, I see no mention of this change in the package release notes, and the documentation still uses the methods that are marked as deprecated. This is very confusing.

Is there a reason why those methods were marked as obsolete? It seems that avoiding those only creates problems, because of missing properties and a more complex input type (CreateIndexModel instead of IndexKeysDefinition, but the first just wraps the second).

Thanks.



 Comments   
Comment by Aniket Manglam [ 08/Oct/20 ]

As per the documentation, the CreateIndexOptions.ExpireAfter Property Gets or sets when documents expire (used with TTL indexes).
https://mongodb.github.io/mongo-csharp-driver/2.5/apidocs/html/P_MongoDB_Driver_CreateIndexOptions_ExpireAfter.htm

Can anybody confirm that the new CreateOneIndexOptions.MaxTime property has the same purpose and can be seamlessly used with TTL indexes as it is not specifically mentioned in the documentation anywhere.

https://mongodb.github.io/mongo-csharp-driver/2.7/apidocs/html/P_MongoDB_Driver_CreateOneIndexOptions_MaxTime.htm

 

Comment by Robert Stam [ 09/Jan/19 ]

Thank you for pointing out that the documentation needs to be updated. I've created a ticket for that.

See:

https://jira.mongodb.org/browse/CSHARP-2475

Comment by Matteo Contrini [ 09/Jan/19 ]

Thanks, that works fine

What about updating the documentation? It's still suggesting to use deprecated method overloads.

Comment by Robert Stam [ 03/Jan/19 ]

When CreateMany was added some options needed to move into the model. CreateOne was changed to use model also for consistency with CreateMany.

The way to create a unique index using the new methods is:

// assuming TDocument is BsonDocument
var keys = Builders<BsonDocument>.IndexKeys.Ascending("x");
var indexOptions = new CreateIndexOptions { Unique = true };
var model = new CreateIndexModel<BsonDocument>(keys, indexOptions);
collection.Indexes.CreateOne(model);

Generated at Wed Feb 07 21:42:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.