From 043aa0b6ef2a1ffea33a45419fb395f218131c37 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 14 Sep 2010 17:02:05 +0200 Subject: [PATCH 1/2] CreateIndex --- source/MongoDB/IMongoCollection_1.cs | 12 +++++++ source/MongoDB/MongoCollection_1.cs | 53 ++++++++++++++++++++++++++++++++++ source/MongoDB/MongoDB.csproj | 1 + 3 files changed, 66 insertions(+), 0 deletions(-) diff --git a/source/MongoDB/IMongoCollection_1.cs b/source/MongoDB/IMongoCollection_1.cs index 9ddfa88..6ed8df2 100644 --- a/source/MongoDB/IMongoCollection_1.cs +++ b/source/MongoDB/IMongoCollection_1.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Linq.Expressions; +using MongoDB.Protocol; namespace MongoDB { @@ -338,5 +340,15 @@ namespace MongoDB /// The document. /// if set to true [safemode]. void Save(object document, bool safemode); + + /// + /// Creates an index + /// + /// + /// The index expression + /// The name of the Index + /// True if the index is unique, false otherwise + /// The direction of the index for multi-column indexes + void CreateIndex(Expression> index, string indexName, bool isUnique, IndexDirection direction); } } \ No newline at end of file diff --git a/source/MongoDB/MongoCollection_1.cs b/source/MongoDB/MongoCollection_1.cs index 4944558..978904b 100644 --- a/source/MongoDB/MongoCollection_1.cs +++ b/source/MongoDB/MongoCollection_1.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Linq.Expressions; using MongoDB.Configuration; using MongoDB.Connections; using MongoDB.Protocol; @@ -556,6 +557,58 @@ namespace MongoDB } /// + /// Creates an index + /// + /// + /// The index expression + /// The name of the Index + /// True if the index is unique, false otherwise + /// The direction of the index for multi-column indexes + public void CreateIndex(Expression> index, string indexName, bool isUnique, IndexDirection direction) + { + var fieldsAndDirections = new Document(); + + // Build document from expression + var exp = index.Body as NewExpression; + if (exp != null) + { + foreach (var x in exp.Arguments.OfType()) + { + string alias = x.Member.Name; + + var classMap = _configuration.MappingStore.GetClassMap(x.Member.DeclaringType); + if (classMap != null) + { + var memberMap = classMap.GetMemberMapFromMemberName(x.Member.Name); + if (memberMap != null) + alias = memberMap.Alias; + } + + fieldsAndDirections[alias] = direction; + } + } + + else if (index.Body is MemberExpression) + { + var me = index.Body as MemberExpression; + string alias = me.Member.Name; + + var classMap = _configuration.MappingStore.GetClassMap(me.Member.DeclaringType); + if (classMap != null) + { + var memberMap = classMap.GetMemberMapFromMemberName(me.Member.Name); + if (memberMap != null) + alias = memberMap.Alias; + } + + fieldsAndDirections[alias] = direction; + } + + // create the index + Metadata.CreateIndex(indexName, fieldsAndDirections, isUnique); + } + + /// /// Checks the error. /// /// if set to true [safemode]. diff --git a/source/MongoDB/MongoDB.csproj b/source/MongoDB/MongoDB.csproj index 78f966c..61fbbac 100644 --- a/source/MongoDB/MongoDB.csproj +++ b/source/MongoDB/MongoDB.csproj @@ -144,6 +144,7 @@ + -- 1.6.4.msysgit.0