How to use the CreateIndex method?

2,164 views
Skip to first unread message

Jeffrey

unread,
Jan 4, 2012, 8:07:12 AM1/4/12
to mongodb-csharp
I am using mongodb-csharp driver version 1.2. There are three methods
to create an index in the class MongoCollection:
CreateIndex(array<String>)
CreateIndex(IMongoIndexKeys)
CreateIndex(IMongoIndexKeys, IMongoIndexOptions)
But I don't know how to use them. Can anyone give me some examples to
illustrate? And when I want to make the index unique or drop
duplicates, what should I do? Thank you!

Robert Stam

unread,
Jan 4, 2012, 9:48:57 AM1/4/12
to mongodb...@googlegroups.com
Use the first method when you want to create a simple index with all ascending keys:

    collection.CreateIndex("a", "b", "c");

Use the second method when you want to specify that some of the keys are descending:

    var keys = IndexKeys.Ascending("a", "b").Descending("c");
    collection.CreateIndex(keys);

Use the third method when you want to set any options for the index (like unique or dropDups):

    var keys = IndexKeys.Ascending("a", "b").Descending("c");
    var options = IndexOptions.SetUnique(true).SetDropDups(true);
    collection.CreateIndex(keys, options);

Jeffrey

unread,
Jan 4, 2012, 10:51:10 PM1/4/12
to mongodb-csharp
If I create an index, is there any way to specify my own index name in
C# driver?

Robert Stam

unread,
Jan 4, 2012, 10:53:39 PM1/4/12
to mongodb...@googlegroups.com
Checkout all the available IndexOptions. You can name your index like this:

    var keys = IndexKeys.Ascending("a", "b", "c");
    var options = IndexOptions.SetName("abcIndex");
    collection.CreateIndex(keys, options);

2012/1/4 Jeffrey <hujun...@gmail.com>

Manuel Sopena Ballesteros

unread,
Apr 5, 2014, 3:37:27 AM4/5/14
to mongodb...@googlegroups.com
Hi Robert,

regarding the third method "collection.CreateIndex(keys, options)" how would you do using powershell?

thank you very much

Robert Stam

unread,
Apr 8, 2014, 12:26:36 PM4/8/14
to mongodb...@googlegroups.com
This worked for me:

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\> Add-Type -Path "C:\mongodb\drivers\csharp\CSharpDriver-1.9.0\MongoDB.Bson.dll"
PS C:\> Add-Type -Path "C:\mongodb\drivers\csharp\CSharpDriver-1.9.0\MongoDB.Driver.dll"
PS C:\> $client = New-Object -TypeName "MongoDB.Driver.MongoClient" "mongodb://localhost"
PS C:\> $server = $client.GetServer()
PS C:\> $database = $server.GetDatabase("test")
PS C:\> $collection = $database.GetCollection("test")
PS C:\> $keys = [MongoDB.Driver.Builders.IndexKeys]::Ascending("a", "b", "c")
PS C:\> $options = [MongoDB.Driver.Builders.IndexOptions]::SetName("abcIndex")
PS C:\> $collection.CreateIndex($keys, $options)


DocumentsAffected   : 0
HasLastErrorMessage : False
LastErrorMessage    :
Upserted            :
UpdatedExisting     : False
Code                :
Command             :
CommandName         :
Response            : {ok=1, n=0}
ErrorMessage        :
Ok                  : True
ServerInstance      :



PS C:\>

You'll have to adjust the paths used in the Add-Type statements depending on where your driver DLLs are located.

Using the MongoDB shell, I ran this before those Powershell statements:

> db.test.drop()
true
> db.test.getIndexes()
[ ]
>

And using the MongoDB shell again afterwards, checked that the index was properly created:

> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.test"
        },
        {
                "v" : 1,
                "key" : {
                        "a" : 1,
                        "b" : 1,
                        "c" : 1
                },
                "name" : "abcIndex",
                "ns" : "test.test"
        }
]
>






--
You received this message because you are subscribed to the Google Groups "mongodb-csharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-cshar...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages