[CSHARP-2421] How can I EnsureIndex with String. Created: 19/Oct/18  Updated: 27/Oct/23  Resolved: 19/Oct/18

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 2.7.0
Fix Version/s: None

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


 Description   

Since EnsureIndexes methods are obsolete

How can I ensure indexes using strings?



 Comments   
Comment by Robert Stam [ 19/Oct/18 ]

collection.EnsureIndex is deprecated and in any case was only available in the Legacy API.

It has been replaced with collection.Indexes.CreateOne

Not sure what you mean by "using strings". If by strings you mean the names of the elements you want to index you can create an index like this:

var keys = Builders<BsonDocument>.IndexKeys.Ascending("x").Ascending("y"); // assuming you are using a collection of BsonDocument
var model = new CreateIndexModel<BsonDocument>(keys);
collection.Indexes.CreateOne(model);

Note: CreateOne is a no-op on the server if the index already exists.

 

 

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