[CSHARP-510] Unhandled exception by creating an inex in a special way Created: 24/Jun/12  Updated: 14/May/14  Resolved: 28/Jun/12

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

Type: Bug Priority: Critical - P2
Reporter: Michael Stupp Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: C#
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win7 / 64 bit (Virtual Box)


Attachments: Text File Program.cs    
Backwards Compatibility: Minor Change

 Description   

Creating an index in like so works fine:
var collection=database.CreateCollection("Categories");
var category = database.GetCollection("Categories");
var keys = IndexKeys.Ascending("Name");
var sort = SortBy.Ascending("Name");
database.GetCollection("Categories").CreateIndex(keys, IndexOptions.SetUnique(true).SetDropDups(false));

But creating an index like so (define the options before the CreateIndex throws an unhandled exception in many situations):
var collection=database.CreateCollection("Categories");
var category = database.GetCollection("Categories");
var keys = IndexKeys.Ascending("Name");
var sort = SortBy.Ascending("Name");
var options = IndexOptions.SetUnique(true).SetDropDups(false);
database.GetCollection("Categories").CreateIndex(keys,options);



 Comments   
Comment by Robert Stam [ 28/Jun/12 ]

@Michael, if you would like to try to reproduce this on your machine using the attached test program let me know what your results are.

Comment by Robert Stam [ 28/Jun/12 ]

Attached test program that calls CreateIndex with and without using a temporary variable in an attempt to reproduce the issue reported.

Comment by Robert Stam [ 28/Jun/12 ]

I am unable to reproduce this. Attached is a simple command line program that consists of only these lines:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
 
namespace TestCSharp510
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            var server = MongoServer.Create("mongodb://localhost/?safe=true");
            var database = server.GetDatabase("test");
            database.Drop();
 
            var keys = IndexKeys.Ascending("Name");
            var options = IndexOptions.SetUnique(true).SetDropDups(false);
            database.GetCollection("categories1").CreateIndex(keys, options);
            database.GetCollection("categories2").CreateIndex(keys, IndexOptions.SetUnique(true).SetDropDups(false));
        }
    }
}

The program creates two indexes on two different collections. The first time using an intermediate variable (which you state throws an unhandled exception) and the second time passing the options directly to the CreateIndex method without an intermediate variable.

I used the mongo shell to verify that both indexes were created identically:

> db.categories1.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "test.categories1",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "Name" : 1
                },
                "unique" : true,
                "ns" : "test.categories1",
                "name" : "Name_1",
                "dropDups" : false
        }
]
> db.categories2.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "test.categories2",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "Name" : 1
                },
                "unique" : true,
                "ns" : "test.categories2",
                "name" : "Name_1",
                "dropDups" : false
        }
]
>

Comment by Robert Stam [ 25/Jun/12 ]

The most immediately useful information is the error message and the full stack trace.

If you do provide a Program.cs file what is helpful is a short command line program (10-20 lines) that reproduces the issue. Don't attach your entire program.

Comment by Robert Stam [ 25/Jun/12 ]

It should make no difference whether you use an intermediate variable to hold the index options or not. In fact, the CreateIndex method can't even tell if you used an intermediate variable or not.

Can you provide the message of the unhandled exception you got and a full stack trace?

Even better, can you attach a Program.cs file to reproduce the issue?

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