@@ -288,35 +288,37 @@ public virtual long Count(IMongoQuery query)
288
288
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
289
289
/// <param name="options">The index options(usually an IndexOptionsDocument or created using the IndexOption builder).</param>
290
290
/// <returns>A WriteConcernResult.</returns>
291
- [ Obsolete ( "Use EnsureIndex instead." ) ]
292
291
public virtual WriteConcernResult CreateIndex ( IMongoIndexKeys keys , IMongoIndexOptions options )
293
292
{
294
- var keysDocument = keys . ToBsonDocument ( ) ;
295
- var optionsDocument = options . ToBsonDocument ( ) ;
296
- var indexes = _database . GetCollection ( "system.indexes" ) ;
297
- var indexName = GetIndexName ( keysDocument , optionsDocument ) ;
298
- var index = new BsonDocument
293
+ using ( _database . RequestStart ( ReadPreference . Primary ) )
299
294
{
300
- { "name" , indexName } ,
301
- { "ns" , FullName } ,
302
- { "key" , keysDocument }
303
- } ;
304
- index . Merge ( optionsDocument ) ;
305
- var insertOptions = new MongoInsertOptions
306
- {
307
- CheckElementNames = false ,
308
- WriteConcern = WriteConcern . Acknowledged
309
- } ;
310
- var result = indexes . Insert ( index , insertOptions ) ;
311
- return result ;
295
+ if ( _server . RequestConnection . ServerInstance . Supports ( FeatureId . CreateIndexCommand ) )
296
+ {
297
+ try
298
+ {
299
+ CreateIndexWithCommand ( keys , options ) ;
300
+ var fakeResponse = new BsonDocument ( "ok" , 1 ) ;
301
+ return new WriteConcernResult ( fakeResponse ) ;
302
+ }
303
+ catch ( MongoCommandException ex )
304
+ {
305
+ var translatedResult = new WriteConcernResult ( ex . CommandResult . Response ) ;
306
+ translatedResult . Command = ex . CommandResult . Command ;
307
+ throw new WriteConcernException ( ex . Message , translatedResult ) ;
308
+ }
309
+ }
310
+ else
311
+ {
312
+ return CreateIndexWithInsert ( keys , options ) ;
313
+ }
314
+ }
312
315
}
313
316
314
317
/// <summary>
315
318
/// Creates an index for this collection.
316
319
/// </summary>
317
320
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
318
321
/// <returns>A WriteConcernResult.</returns>
319
- [ Obsolete ( "Use EnsureIndex instead." ) ]
320
322
public virtual WriteConcernResult CreateIndex ( IMongoIndexKeys keys )
321
323
{
322
324
return CreateIndex ( keys , IndexOptions . Null ) ;
@@ -327,7 +329,6 @@ public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys)
327
329
/// </summary>
328
330
/// <param name="keyNames">The names of the indexed fields.</param>
329
331
/// <returns>A WriteConcernResult.</returns>
330
- [ Obsolete ( "Use EnsureIndex instead." ) ]
331
332
public virtual WriteConcernResult CreateIndex ( params string [ ] keyNames )
332
333
{
333
334
return CreateIndex ( IndexKeys . Ascending ( keyNames ) ) ;
@@ -487,33 +488,30 @@ public virtual CommandResult DropIndexByName(string indexName)
487
488
/// </summary>
488
489
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
489
490
/// <param name="options">The index options(usually an IndexOptionsDocument or created using the IndexOption builder).</param>
491
+ [ Obsolete ( "Use CreateIndex instead." ) ]
490
492
public virtual void EnsureIndex ( IMongoIndexKeys keys , IMongoIndexOptions options )
491
493
{
492
- #pragma warning disable 618
493
494
CreateIndex ( keys , options ) ;
494
- #pragma warning restore
495
495
}
496
496
497
497
/// <summary>
498
498
/// Ensures that the desired index exists and creates it if it does not.
499
499
/// </summary>
500
500
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
501
+ [ Obsolete ( "Use CreateIndex instead." ) ]
501
502
public virtual void EnsureIndex ( IMongoIndexKeys keys )
502
503
{
503
- #pragma warning disable 618
504
504
CreateIndex ( keys ) ;
505
- #pragma warning restore
506
505
}
507
506
508
507
/// <summary>
509
508
/// Ensures that the desired index exists and creates it if it does not.
510
509
/// </summary>
511
510
/// <param name="keyNames">The names of the indexed fields.</param>
511
+ [ Obsolete ( "Use CreateIndex instead." ) ]
512
512
public virtual void EnsureIndex ( params string [ ] keyNames )
513
513
{
514
- #pragma warning disable 618
515
514
CreateIndex ( keyNames ) ;
516
- #pragma warning restore
517
515
}
518
516
519
517
/// <summary>
@@ -2159,6 +2157,30 @@ private void AssignId(InsertRequest request)
2159
2157
}
2160
2158
}
2161
2159
2160
+ private CommandResult CreateIndexWithCommand ( IMongoIndexKeys keys , IMongoIndexOptions options )
2161
+ {
2162
+ var command = new CommandDocument
2163
+ {
2164
+ { "createIndexes" , Name } ,
2165
+ { "indexes" , new BsonArray { GetIndexDocument ( keys , options ) } }
2166
+ } ;
2167
+
2168
+ return RunCommandAs < CommandResult > ( command ) ;
2169
+ }
2170
+
2171
+ private WriteConcernResult CreateIndexWithInsert ( IMongoIndexKeys keys , IMongoIndexOptions options )
2172
+ {
2173
+ var index = GetIndexDocument ( keys , options ) ;
2174
+ var insertOptions = new MongoInsertOptions
2175
+ {
2176
+ CheckElementNames = false ,
2177
+ WriteConcern = WriteConcern . Acknowledged
2178
+ } ;
2179
+ var indexes = _database . GetCollection ( "system.indexes" ) ;
2180
+ var result = indexes . Insert ( index , insertOptions ) ;
2181
+ return result ;
2182
+ }
2183
+
2162
2184
private MongoCursor FindAs ( Type documentType , IMongoQuery query , IBsonSerializer serializer , IBsonSerializationOptions serializationOptions )
2163
2185
{
2164
2186
return MongoCursor . Create ( documentType , this , query , _settings . ReadPreference , serializer , serializationOptions ) ;
@@ -2197,6 +2219,22 @@ private BsonBinaryWriterSettings GetBinaryWriterSettings()
2197
2219
} ;
2198
2220
}
2199
2221
2222
+ private BsonDocument GetIndexDocument ( IMongoIndexKeys keys , IMongoIndexOptions options )
2223
+ {
2224
+ var keysDocument = keys . ToBsonDocument ( ) ;
2225
+ var optionsDocument = options . ToBsonDocument ( ) ;
2226
+ var indexName = GetIndexName ( keysDocument , optionsDocument ) ;
2227
+ var index = new BsonDocument
2228
+ {
2229
+ { "ns" , FullName } ,
2230
+ { "name" , indexName } ,
2231
+ { "key" , keysDocument }
2232
+ } ;
2233
+ index . Merge ( optionsDocument ) ;
2234
+
2235
+ return index ;
2236
+ }
2237
+
2200
2238
private string GetIndexName ( BsonDocument keys , BsonDocument options )
2201
2239
{
2202
2240
if ( options != null )
0 commit comments