MongoDB an C#

163 views
Skip to first unread message

Developer

unread,
Apr 22, 2012, 8:33:52 AM4/22/12
to mongodb-user
Hi, my name is Michael and I just read a book about MongoDB. "Wow,
that's it. " I thought not knowing the big barrier and errors of the
C# driver.
I started to code a Dameon Gui that worked fine. After that I just
wanted to start in my new program to set a unique Index. But it does
not work. The index is accepted but I can save the value as often as I
want.
I found much more errors about the C# driver. Can anybody tell me a
website where I can find half-way useable tips hints or coding
examples for the combination of MongoDB and C#. Or does anybody know
an ebook or another book for learning this?

thx and best regards
Michael

Robert Stam

unread,
Apr 22, 2012, 10:00:59 AM4/22/12
to mongod...@googlegroups.com
The home page for information about the official C# driver is here:


Not sure what problems you were having since you didn't show any code samples, but here's a quick sample of creating a unique index:

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

    var server = MongoServer.Create("mongodb://localhost/?safe=true");
    var database = server.GetDatabase("test");
    var collection = database.GetCollection<C>("test");

    collection.CreateIndex(
        IndexKeys.Ascending("x"),
        IndexOptions.SetUnique(true)
    );

and here's a quick test attempting to insert two documents with the same "x" value:

    collection.Insert(new BsonDocument("x", 1));
    collection.Insert(new BsonDocument("x", 1));

The first Insert succeeds and the second throws an exception because of the duplicate "x" value.

Let me know if you have any other specific questions and I'd be happy to help.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Michael stupp

unread,
Apr 22, 2012, 11:45:35 AM4/22/12
to mongod...@googlegroups.com
Hi Robert,
 
I will try this way the next days because at the moment I'm very frustrated.
Best regards
Michael


 
2012/4/22 Robert Stam <rob...@10gen.com>

Robert Stam

unread,
Apr 22, 2012, 11:49:54 AM4/22/12
to mongod...@googlegroups.com
Sorry to hear you are feeling frustrated.

Let me know if I can be of any help. I think once you get past the first few steps you'll find it isn't that hard after all.

One of the early decisions you need to make is whether you want to work with the BsonDocument object model directly or whether you want to create your own domain model C# classes and let the driver map them to and from BSON documents in the database. Most people choose to work with their own C# classes instead of with BsonDocuments directly.

Regards.

Michael stupp

unread,
Apr 23, 2012, 2:16:52 AM4/23/12
to mongod...@googlegroups.com
Hi,

I think thats the problem. I tried to use my own class named category.
With that class I created a collection with the unique key "Name"

public class Category

{

public string Name { get; set; }

}

Here is the code where I am using it

MongoServer= server;

MongoDatabase DO;


server =MongoServer.Create();

server.Connect();

DO = server.GetDatabase("DO");

MongoCollection<Category> Category = DO.GetCollection<Category>("Category");

var keys = IndexKeys.Ascending("Name");

var options = IndexOptions.SetUnique(true).SetDropDups(true);

Category.CreateIndex(keys, options);

BUT

Category cat = new Category();
Category.Insert(cat);
Category cat = new Category();
Category.Insert(´Test');

works without an exception! How I have to let the driver map my classes to BSON?

Robert Stam

unread,
Apr 23, 2012, 11:35:31 AM4/23/12
to mongod...@googlegroups.com
Your Insert statement should look like this:

    Category.Insert(cat); // use cat, not "Test"

Although I would recommend against using capitalized variable names.

I reproduced your scenario with the following two Insert statements:

    collection.Insert(new Category { Name = "Test" }); // works
    collection.Insert(new Category { Name = "Test" }); // throws exception because Name is duplicate

You didn't mention if you are using SafeMode or not, but I recommend that you verify that SafeMode is turned on in your connection string, as in:

    var connetionString = "mongodb://localhost/?safe=true";

Turning SafeMode on turns on full error checking.

The full test program is here so you can compare it to yours:

Robert Stam

unread,
Apr 23, 2012, 7:25:37 PM4/23/12
to mongod...@googlegroups.com
I've written a new quick-start page for the C# driver. It's at:


I'm hoping you (and others) find this new short quick-start helpful.

Michael stupp

unread,
Apr 23, 2012, 11:08:19 PM4/23/12
to mongod...@googlegroups.com
Thank you very much!
best regards
Michael

2012/4/24 Robert Stam <rob...@10gen.com>:

Michael stupp

unread,
Apr 25, 2012, 6:53:38 AM4/25/12
to mongod...@googlegroups.com
Sorry Tom,

I must nerv you again. Look at my small code, waht is wrong? Aftre
settting the unique Index the Category "Eingescannt" should not
created a second time without throwing an exception!

void Start()
{
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("DO");
var collection = database.GetCollection<Category>("Category");


var keys = IndexKeys.Ascending("Name");
var options = IndexOptions.SetUnique(true).SetDropDups(true);

// Wenn es die Kategorie noch nicht gibt diese anlegen
var Category = new Category { Name = "Eingescannt" };
collection.Insert(Category);
var id = Category.Id;
Category = new Category { Name = "Eingescannt" };
collection.Insert(Category);
id = Category.Id;
server.Disconnect();
}

2012/4/24 Michael stupp <michae...@googlemail.com>:

Robert Stam

unread,
Apr 25, 2012, 7:53:42 AM4/25/12
to mongod...@googlegroups.com
You haven't actually created the index. After where you set the keys and index variables, add this line of code:

    collection.CreateIndex(keys, options);

Michael stupp

unread,
Apr 25, 2012, 8:32:46 AM4/25/12
to mongod...@googlegroups.com
oh my god...thx

2012/4/25 Robert Stam <rob...@10gen.com>:

Michael stupp

unread,
Apr 25, 2012, 11:03:45 AM4/25/12
to mongod...@googlegroups.com
Sorry but that didn't work. No exception and both categorys are
created in the collection. No wmy frustrating level is very high.
Are you sure there is no problem with the C# driver?

Best regards Michael


2012/4/25 Michael stupp <michae...@googlemail.com>:

Robert Stam

unread,
Apr 25, 2012, 11:07:50 AM4/25/12
to mongod...@googlegroups.com
Can you show me your entire program?

I'll run it on my machine and see what's happening.

Sorry about the frustration level.

Robert Stam

unread,
Apr 25, 2012, 11:14:48 AM4/25/12
to mongod...@googlegroups.com
I incorporated your Start routine into a full program (and added the call to CreateIndex). Here's the full program:


I get an exception on the second call to Insert. Here's the full stack trace:

Unhandled exception:
MongoDB.Driver.MongoSafeModeException: Safemode detected an error 'E11000 duplic
ate key error index: DO.Category.$Name_1  dup key: { : "Eingescannt" }'. (Respon
se was { "err" : "E11000 duplicate key error index: DO.Category.$Name_1  dup key
: { : \"Eingescannt\" }", "code" : 11000, "n" : 0, "connectionId" : 192, "ok" :
1.0 }).
   at MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage me
ssage, SafeMode safeMode) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\In
ternal\MongoConnection.cs:line 509
   at MongoDB.Driver.MongoCollection.InsertBatch(Type nominalType, IEnumerable d
ocuments, MongoInsertOptions options) in C:\work\10gen\mongodb\mongo-csharp-driv
er\Driver\Core\MongoCollection.cs:line 1104
   at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document, M
ongoInsertOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\C
ore\MongoCollection.cs:line 942
   at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document) i
n C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs:line
926
   at MongoDB.Driver.MongoCollection.Insert[TNominalType](TNominalType document)
 in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs:lin
e 890
   at MongoDB.Driver.MongoCollection`1.Insert(TDefaultDocument document) in C:\w
ork\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs:line 1738
   at ConsoleApplication1.Program.Start() in C:\Users\rstam\Documents\Visual Stu
dio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 51
   at ConsoleApplication1.Program.Main(String[] args) in C:\Users\rstam\Document
s\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs
:line 24
Press Enter to continue

Compare my test program to your program to see what's different.

Michael stupp

unread,
Apr 25, 2012, 11:31:46 AM4/25/12
to mongod...@googlegroups.com
classes.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Document_omnivore
{

public class Category
{
public ObjectId Id { get; set; }

public string Name { get; set; }
}

public class Document
{
public string Name { get; set; }
public string Extension { get; set; }
}
}


main.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Security.Cryptography;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.IdGenerators;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Wrappers;

namespace Document_omnivore
{

public partial class Main : Form
{

public Main()
{
InitializeComponent();
Start();

}

void Start()
{
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("DO");
var collection = database.GetCollection<Category>("Category");
var keys = IndexKeys.Ascending("Name");
var options = IndexOptions.SetUnique(true).SetDropDups(true);
collection.CreateIndex(keys, options);

// Wenn es die Kategorie noch nicht gibt diese anlegen
var Category = new Category { Name = "Eingescannt" };
collection.Insert(Category);
var id = Category.Id;
Category = new Category { Name = "Eingescannt" };
collection.Insert(Category);
id = Category.Id;
server.Disconnect();
}
}

program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Document_omnivore
{
static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
}
}
}




That's all. Compiled with Visual Studio 2010

2012/4/25 Robert Stam <rob...@10gen.com>:

Michael stupp

unread,
Apr 25, 2012, 11:46:51 AM4/25/12
to mongod...@googlegroups.com
Yes, but there is no exception thrown.

2012/4/25 Robert Stam <rob...@10gen.com>:
> Did you look at the test program I used to verify that the exception is
> being thrown?
>
> Your program is a Windows Forms application. Is it possible that the
> exception is being thrown but you are just not catching it or seeing it?
>
>
> On Wed, Apr 25, 2012 at 11:31 AM, Michael stupp

Robert Stam

unread,
Apr 25, 2012, 11:51:26 AM4/25/12
to mongod...@googlegroups.com
I am unable to compile your program. There seems to be something missing (like the rest of the Main partial class where InitializeComponent is defined).

Do you want to try zipping up your entire solution folder and sending that to me?

Michael stupp

unread,
Apr 25, 2012, 11:54:21 AM4/25/12
to mongod...@googlegroups.com
Ok, wait a moment. It's on a virtual machine.
Attached you will find the parameteres how I start the Daemoen.
Daemon.jpg

craiggwilson

unread,
Apr 25, 2012, 11:57:09 AM4/25/12
to mongod...@googlegroups.com
I created my own little test that is identical to Robert's and I get an exception on the second insert.  Could you provide some more information about your environment?  What version of the server are you using?  What version of the driver?  What operating system are you using?

>> >>> >>> >>>> >>>> For more options, visit this group at
>> >>> >>> >>>> >>>> http://groups.google.com/group/mongodb-user?hl=en.
>> >>> >>> >>>> >>>>
>> >>> >>> >>>> >>>
>> >>> >>> >>>> >>> --
>> >>> >>> >>>> >>> You received this message because you are subscribed to
>> >>> >>> >>>> >>> the
>> >>> >>> >>>> >>> Google
>> >>> >>> >>>> >>> Groups "mongodb-user" group.
>> >>> >>> >>>> >>> To post to this group, send email to
>> >>> >>> >>>> >>> mongod...@googlegroups.com.
>> >>> >>> >>>> >>> To unsubscribe from this group, send email to

>> >>> >>> >>>> >>> For more options, visit this group at
>> >>> >>> >>>> >>> http://groups.google.com/group/mongodb-user?hl=en.
>> >>> >>> >>>> >>
>> >>> >>> >>>> >>
>> >>> >>> >>>> >> --
>> >>> >>> >>>> >> You received this message because you are subscribed to
>> >>> >>> >>>> >> the
>> >>> >>> >>>> >> Google
>> >>> >>> >>>> >> Groups "mongodb-user" group.
>> >>> >>> >>>> >> To post to this group, send email to
>> >>> >>> >>>> >> mongod...@googlegroups.com.
>> >>> >>> >>>> >> To unsubscribe from this group, send email to

>> >>> >>> >>>> >> For more options, visit this group at
>> >>> >>> >>>> >> http://groups.google.com/group/mongodb-user?hl=en.
>> >>> >>> >>>> >
>> >>> >>> >>>> >
>> >>> >>> >>>> > --
>> >>> >>> >>>> > You received this message because you are subscribed to the
>> >>> >>> >>>> > Google
>> >>> >>> >>>> > Groups "mongodb-user" group.
>> >>> >>> >>>> > To post to this group, send email to
>> >>> >>> >>>> > mongod...@googlegroups.com.
>> >>> >>> >>>> > To unsubscribe from this group, send email to

>> >>> >>> >>>> > For more options, visit this group at
>> >>> >>> >>>> > http://groups.google.com/group/mongodb-user?hl=en.
>> >>> >>> >>>>
>> >>> >>> >>>> --
>> >>> >>> >>>> You received this message because you are subscribed to the
>> >>> >>> >>>> Google
>> >>> >>> >>>> Groups
>> >>> >>> >>>> "mongodb-user" group.
>> >>> >>> >>>> To post to this group, send email to
>> >>> >>> >>>> mongod...@googlegroups.com.
>> >>> >>> >>>> To unsubscribe from this group, send email to

>> >>> >>> >>>> For more options, visit this group at
>> >>> >>> >>>> http://groups.google.com/group/mongodb-user?hl=en.
>> >>> >>> >>>>
>> >>> >>> >>>
>> >>> >>> >>
>> >>> >>> >> --
>> >>> >>> >> You received this message because you are subscribed to the
>> >>> >>> >> Google
>> >>> >>> >> Groups
>> >>> >>> >> "mongodb-user" group.
>> >>> >>> >> To post to this group, send email to
>> >>> >>> >> mongod...@googlegroups.com.
>> >>> >>> >> To unsubscribe from this group, send email to

>> >>> >>> >> For more options, visit this group at
>> >>> >>> >> http://groups.google.com/group/mongodb-user?hl=en.
>> >>> >>>
>> >>> >>> --
>> >>> >>> You received this message because you are subscribed to the Google
>> >>> >>> Groups
>> >>> >>> "mongodb-user" group.
>> >>> >>> To post to this group, send email to
>> >>> >>> mongod...@googlegroups.com.
>> >>> >>> To unsubscribe from this group, send email to

>> >>> >>> For more options, visit this group at
>> >>> >>> http://groups.google.com/group/mongodb-user?hl=en.
>> >>> >>>
>> >>> >>
>> >>> >> --
>> >>> >> You received this message because you are subscribed to the Google
>> >>> >> Groups
>> >>> >> "mongodb-user" group.
>> >>> >> To post to this group, send email to mongod...@googlegroups.com.
>> >>> >> To unsubscribe from this group, send email to

>> >>> >> For more options, visit this group at
>> >>> >> http://groups.google.com/group/mongodb-user?hl=en.
>> >>>
>> >>> --
>> >>> You received this message because you are subscribed to the Google
>> >>> Groups
>> >>> "mongodb-user" group.
>> >>> To post to this group, send email to mongod...@googlegroups.com.
>> >>> To unsubscribe from this group, send email to

>> >>> For more options, visit this group at
>> >>> http://groups.google.com/group/mongodb-user?hl=en.
>> >>>
>> >>
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "mongodb-user" group.
>> > To post to this group, send email to mongod...@googlegroups.com.
>> > To unsubscribe from this group, send email to

>> > For more options, visit this group at
>> > http://groups.google.com/group/mongodb-user?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "mongodb-user" group.
>> To post to this group, send email to mongod...@googlegroups.com.
>> To unsubscribe from this group, send email to

>> For more options, visit this group at
>> http://groups.google.com/group/mongodb-user?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to

> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.

Michael stupp

unread,
Apr 25, 2012, 12:01:44 PM4/25/12
to mongod...@googlegroups.com
Here is the zip file
Digital Omnivore.zip

Michael stupp

unread,
Apr 25, 2012, 12:05:07 PM4/25/12
to mongod...@googlegroups.com
Hi,

in the meanwhile I send Robert my complete program as zip file. I'm
using the newest MongoDB (2.0.4)and C# driver files on a Windows 64bit
PC with 8MB RAM.

Best regards Michael

2012/4/25 craiggwilson <craigg...@gmail.com>:
>>> >> >>> >>> >>>> >>>> mongodb-user...@googlegroups.com.
>>> >> >>> >>> >>>> >>>> For more options, visit this group at
>>> >> >>> >>> >>>> >>>> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>> >>> >>>> >>>>
>>> >> >>> >>> >>>> >>>
>>> >> >>> >>> >>>> >>> --
>>> >> >>> >>> >>>> >>> You received this message because you are subscribed
>>> >> >>> >>> >>>> >>> to
>>> >> >>> >>> >>>> >>> the
>>> >> >>> >>> >>>> >>> Google
>>> >> >>> >>> >>>> >>> Groups "mongodb-user" group.
>>> >> >>> >>> >>>> >>> To post to this group, send email to
>>> >> >>> >>> >>>> >>> mongod...@googlegroups.com.
>>> >> >>> >>> >>>> >>> To unsubscribe from this group, send email to
>>> >> >>> >>> >>>> >>> mongodb-user...@googlegroups.com.
>>> >> >>> >>> >>>> >>> For more options, visit this group at
>>> >> >>> >>> >>>> >>> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>> >>> >>>> >>
>>> >> >>> >>> >>>> >>
>>> >> >>> >>> >>>> >> --
>>> >> >>> >>> >>>> >> You received this message because you are subscribed
>>> >> >>> >>> >>>> >> to
>>> >> >>> >>> >>>> >> the
>>> >> >>> >>> >>>> >> Google
>>> >> >>> >>> >>>> >> Groups "mongodb-user" group.
>>> >> >>> >>> >>>> >> To post to this group, send email to
>>> >> >>> >>> >>>> >> mongod...@googlegroups.com.
>>> >> >>> >>> >>>> >> To unsubscribe from this group, send email to
>>> >> >>> >>> >>>> >> mongodb-user...@googlegroups.com.
>>> >> >>> >>> >>>> >> For more options, visit this group at
>>> >> >>> >>> >>>> >> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>> >>> >>>> >
>>> >> >>> >>> >>>> >
>>> >> >>> >>> >>>> > --
>>> >> >>> >>> >>>> > You received this message because you are subscribed to
>>> >> >>> >>> >>>> > the
>>> >> >>> >>> >>>> > Google
>>> >> >>> >>> >>>> > Groups "mongodb-user" group.
>>> >> >>> >>> >>>> > To post to this group, send email to
>>> >> >>> >>> >>>> > mongod...@googlegroups.com.
>>> >> >>> >>> >>>> > To unsubscribe from this group, send email to
>>> >> >>> >>> >>>> > mongodb-user...@googlegroups.com.
>>> >> >>> >>> >>>> > For more options, visit this group at
>>> >> >>> >>> >>>> > http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>> >>> >>>>
>>> >> >>> >>> >>>> --
>>> >> >>> >>> >>>> You received this message because you are subscribed to
>>> >> >>> >>> >>>> the
>>> >> >>> >>> >>>> Google
>>> >> >>> >>> >>>> Groups
>>> >> >>> >>> >>>> "mongodb-user" group.
>>> >> >>> >>> >>>> To post to this group, send email to
>>> >> >>> >>> >>>> mongod...@googlegroups.com.
>>> >> >>> >>> >>>> To unsubscribe from this group, send email to
>>> >> >>> >>> >>>> mongodb-user...@googlegroups.com.
>>> >> >>> >>> >>>> For more options, visit this group at
>>> >> >>> >>> >>>> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>> >>> >>>>
>>> >> >>> >>> >>>
>>> >> >>> >>> >>
>>> >> >>> >>> >> --
>>> >> >>> >>> >> You received this message because you are subscribed to the
>>> >> >>> >>> >> Google
>>> >> >>> >>> >> Groups
>>> >> >>> >>> >> "mongodb-user" group.
>>> >> >>> >>> >> To post to this group, send email to
>>> >> >>> >>> >> mongod...@googlegroups.com.
>>> >> >>> >>> >> To unsubscribe from this group, send email to
>>> >> >>> >>> >> mongodb-user...@googlegroups.com.
>>> >> >>> >>> >> For more options, visit this group at
>>> >> >>> >>> >> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>> >>>
>>> >> >>> >>> --
>>> >> >>> >>> You received this message because you are subscribed to the
>>> >> >>> >>> Google
>>> >> >>> >>> Groups
>>> >> >>> >>> "mongodb-user" group.
>>> >> >>> >>> To post to this group, send email to
>>> >> >>> >>> mongod...@googlegroups.com.
>>> >> >>> >>> To unsubscribe from this group, send email to
>>> >> >>> >>> mongodb-user...@googlegroups.com.
>>> >> >>> >>> For more options, visit this group at
>>> >> >>> >>> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>> >>>
>>> >> >>> >>
>>> >> >>> >> --
>>> >> >>> >> You received this message because you are subscribed to the
>>> >> >>> >> Google
>>> >> >>> >> Groups
>>> >> >>> >> "mongodb-user" group.
>>> >> >>> >> To post to this group, send email to
>>> >> >>> >> mongod...@googlegroups.com.
>>> >> >>> >> To unsubscribe from this group, send email to
>>> >> >>> >> mongodb-user...@googlegroups.com.
>>> >> >>> >> For more options, visit this group at
>>> >> >>> >> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>>
>>> >> >>> --
>>> >> >>> You received this message because you are subscribed to the Google
>>> >> >>> Groups
>>> >> >>> "mongodb-user" group.
>>> >> >>> To post to this group, send email to
>>> >> >>> mongod...@googlegroups.com.
>>> >> >>> To unsubscribe from this group, send email to
>>> >> >>> mongodb-user...@googlegroups.com.
>>> >> >>> For more options, visit this group at
>>> >> >>> http://groups.google.com/group/mongodb-user?hl=en.
>>> >> >>>
>>> >> >>
>>> >> >
>>> >> > --
>>> >> > You received this message because you are subscribed to the Google
>>> >> > Groups
>>> >> > "mongodb-user" group.
>>> >> > To post to this group, send email to mongod...@googlegroups.com.
>>> >> > To unsubscribe from this group, send email to
>>> >> > mongodb-user...@googlegroups.com.
>>> >> > For more options, visit this group at
>>> >> > http://groups.google.com/group/mongodb-user?hl=en.
>>> >>
>>> >> --
>>> >> You received this message because you are subscribed to the Google
>>> >> Groups
>>> >> "mongodb-user" group.
>>> >> To post to this group, send email to mongod...@googlegroups.com.
>>> >> To unsubscribe from this group, send email to
>>> >> mongodb-user...@googlegroups.com.
>>> >> For more options, visit this group at
>>> >> http://groups.google.com/group/mongodb-user?hl=en.
>>> >>
>>> >
>>> > --
>>> > You received this message because you are subscribed to the Google
>>> > Groups
>>> > "mongodb-user" group.
>>> > To post to this group, send email to mongod...@googlegroups.com.
>>> > To unsubscribe from this group, send email to
>>> > mongodb-user...@googlegroups.com.
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/mongodb-user?hl=en.
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "mongodb-user" group.
>>> To post to this group, send email to mongod...@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> mongodb-user...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/mongodb-user?hl=en.
>>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/wzv8jLol_Q0J.
>
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.

Robert Stam

unread,
Apr 25, 2012, 11:36:13 AM4/25/12
to mongod...@googlegroups.com
Did you look at the test program I used to verify that the exception is being thrown?

Your program is a Windows Forms application. Is it possible that the exception is being thrown but you are just not catching it or seeing it?

craiggwilson

unread,
Apr 25, 2012, 12:47:45 PM4/25/12
to mongod...@googlegroups.com
I have downloaded your zip file.  I had to pull down the nuget package for mongodb as you didn't include those binaries, which is version 1.4.1.  Other than that, I ran your program and got a message box that said "Double Key".  I believe this is what you were expecting, so I don't know what say at this point.  

That being said, given what your program appears to be doing, relying on the database to prove uniqueness is not the best way to handle this problem.  This is a business convern, not  a data issue and therefore should be checked as business logic, not at the database.  I suggest that you check this type of constraint with a query first and let the database handle exceptional circumstances.  This will also set you up for sharding in the future if it ever gets big enough to need that.

Michael stupp

unread,
Apr 25, 2012, 1:07:06 PM4/25/12
to mongod...@googlegroups.com
Ok,

maybe this is more a business logic than a database problem but anyway
this code should work. And before I start a big project I want to know
why
this does not work. Can it be a start parameter for the daemon?

2012/4/25 craiggwilson <craigg...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/EZLMFVRS49gJ.

craiggwilson

unread,
Apr 25, 2012, 1:11:49 PM4/25/12
to mongod...@googlegroups.com
Your code, verbatim, does work for me.  It breaks where it should and inserts where it should.  I'm wondering what is different between your environment and mine that would cause the exact same code to behave differently.

Regarding your other question: can "what" be a start parameter for the daemon?  And what daemon are you talking about?

Michael stupp

unread,
Apr 25, 2012, 1:23:47 PM4/25/12
to mongod...@googlegroups.com
I mean the mongodb daemon. Maybe we are using different parameters.
Also I copied the Mongodb files from drive c to drive d but this
should not
be the reason for such a behavior.


2012/4/25 craiggwilson <craigg...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/M8N9-UkwnYYJ.

craiggwilson

unread,
Apr 25, 2012, 1:46:14 PM4/25/12
to mongod...@googlegroups.com
Would you mind terribly taking the code Robert posted above and running it?

Michael stupp

unread,
Apr 26, 2012, 12:28:02 AM4/26/12
to mongod...@googlegroups.com
I did that. It opens a DOS-BOX and "press enter". No exception.
I resume: I'm testing on a Windows 7 64bit PC with 64bit MongoDB and
32bit C# drivers.

2012/4/25 craiggwilson <craigg...@gmail.com>:
> Would you mind terribly taking the code Robert posted above and running it?
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/rS63v2SZSmIJ.
Message has been deleted

craiggwilson

unread,
Apr 26, 2012, 9:42:18 AM4/26/12
to mongod...@googlegroups.com
Can you launch a shell (mongo.exe) and type in the below commands and let me know the output.  This will change to the DO database and show the indexes that currently exist on the Category collection.

use DO
db.Category.getIndexes()



On Wednesday, April 25, 2012 11:28:02 PM UTC-5, Developer wrote:
I did that. It opens a DOS-BOX and "press enter". No exception.
I resume: I'm testing on a Windows 7 64bit PC with 64bit MongoDB and
32bit C# drivers.

2012/4/25 craiggwilson <craigg...@gmail.com>:
> Would you mind terribly taking the code Robert posted above and running it?
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/rS63v2SZSmIJ.
>
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to

Michael stupp

unread,
Apr 26, 2012, 11:14:05 AM4/26/12
to mongod...@googlegroups.com
Sure, attached you will find the screenshot.


2012/4/26 craiggwilson <craigg...@gmail.com>:
> Can you launch a shell (mongo.exe) and type in the below commands and let me
> know the output.  This will change to the DO database and show the indexes
> that currently exist on the Category collection.
>
> use DO
> db.Category.getIndexes()
>
>
>
> On Wednesday, April 25, 2012 11:28:02 PM UTC-5, Developer wrote:
>>
>> I did that. It opens a DOS-BOX and "press enter". No exception.
>> I resume: I'm testing on a Windows 7 64bit PC with 64bit MongoDB and
>> 32bit C# drivers.
>>
>> 2012/4/25 craiggwilson <craigg...@gmail.com>:
>> > Would you mind terribly taking the code Robert posted above and running
>> > it?
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "mongodb-user" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/mongodb-user/-/rS63v2SZSmIJ.
>> >
>> > To post to this group, send email to mongod...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > mongodb-user...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/mongodb-user?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/ztBdvwksDggJ.
>
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
mongo.JPG

craiggwilson

unread,
Apr 26, 2012, 11:21:04 AM4/26/12
to mongod...@googlegroups.com
Tremendous.  I think this is your problem.  There is a pre-existing index on this collection.  Can you run the following from the shell and then try your program again.  This will drop the Category collection, and thereby remove the indexes associated with it.  When your program runs again, the indexes will get created as specified by your program and you should receive the expected results.

use DO
db.Category.drop()


Let us know how it goes.


On Thursday, April 26, 2012 10:14:05 AM UTC-5, Developer wrote:
Sure, attached you will find the screenshot.


2012/4/26 craiggwilson <craigg...@gmail.com>:
> Can you launch a shell (mongo.exe) and type in the below commands and let me
> know the output.  This will change to the DO database and show the indexes
> that currently exist on the Category collection.
>
> use DO
> db.Category.getIndexes()
>
>
>
> On Wednesday, April 25, 2012 11:28:02 PM UTC-5, Developer wrote:
>>
>> I did that. It opens a DOS-BOX and "press enter". No exception.
>> I resume: I'm testing on a Windows 7 64bit PC with 64bit MongoDB and
>> 32bit C# drivers.
>>
>> 2012/4/25 craiggwilson <craigg...@gmail.com>:
>> > Would you mind terribly taking the code Robert posted above and running
>> > it?
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "mongodb-user" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/mongodb-user/-/rS63v2SZSmIJ.
>> >
>> > To post to this group, send email to mongod...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > For more options, visit this group at
>> > http://groups.google.com/group/mongodb-user?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/ztBdvwksDggJ.
>
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to

Michael stupp

unread,
Apr 26, 2012, 12:43:44 PM4/26/12
to mongod...@googlegroups.com
That's it. This was the solution. No matter why. But now it works.
Many thanks for your time and help

Greeting rom Germany

2012/4/26 craiggwilson <craigg...@gmail.com>:
>> >> > mongodb-user...@googlegroups.com.
>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/mongodb-user?hl=en.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "mongodb-user" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/mongodb-user/-/ztBdvwksDggJ.
>> >
>> > To post to this group, send email to mongod...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > mongodb-user...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/mongodb-user?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/7SROX9u5PyIJ.
>
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages