[CSHARP-64] Add strongly typed CreateIndex() methods Created: 14/Sep/10  Updated: 19/Oct/16  Resolved: 20/Oct/10

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

Type: Improvement Priority: Major - P3
Reporter: Oliver Weichhold Assignee: Sam Corder
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File 0001-CreateIndex.patch     Text File 0002-CreateIndex.patch    

 Description   

The current way of creating indexes using string arguments is neither intuitive nor provides it compile time checking. It would be good to be able to create indexes in a strongly typed fashion.

var col = db.GetCollection<User>();
var meta = col.Metadata;
col.CreateIndex(x => x.Username, "idx_username", true, IndexOption.Ascending);



 Comments   
Comment by Steve Wagner [ 20/Oct/10 ]

Migrated to GitHub http://github.com/mongodb-csharp/mongodb-csharp/issues/issue/9

Comment by Oliver Weichhold [ 14/Sep/10 ]

I took the liberty and implemented it myself against the latest HEAD. Perhaps its of some use.

Usage:

public class User
{
public Guid Id

{ get; set; }
public string Username { get; set; }

public string Password

{ get; set; }

}

var col = db.GetCollection<User>();

// Single Column Index
col.CreateIndex(x => x.Username, "idx_username", true, IndexDirection.Ascending);

// Composite Index
col.CreateIndex(x => new

{x.Username, x.Password }

, "idx_username_password", true, IndexDirection.Ascending);

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