Can't enumerate over objects with MongoDBRef

17 views
Skip to first unread message

rupurt

unread,
Dec 13, 2010, 9:02:47 AM12/13/10
to mongodb-csharp
I have a collection called Products which I am trying to enumerate
using the official mongo-csharp driver. However as soon as I try to
enumerate the collection (e.g. with a foreach loop) I get the
following error.

"Default constructor not found for type MongoDB.Driver.MongoDBRef"

The entity class looks like this

public partial class Product
{
public BsonObjectId _id { get; set; }

public string Name { get; set; }

public string Description { get; set; }

public string Url { get; set; }

public int Price { get; set; }

public string Country { get; set; }

public MongoDBRef Merchant { get; set; }
}

The entry in the collection looks like the following

{
"_id" : ObjectId("4cff739fba63c20301ee5bc5"),
"Name" : "Product Name",
"Description" : "Product Description",
"Url" : "http://mysite.com/products/product-name",
"Price" : 1200,
"Country" : "au",
"Merchant" : {
"$ref" : "Merchant",
"$id" : ObjectId("533981033d565e640d000000")
}
}

And i'm reading it in like this.

var db = Db.Instance.GetDatabase();
var matches = db.GetCollection<Product>("Product").FindAll();

I don't get the error until I do either of the following.

var l = matches.ToList();

OR

foreach (var p in matches) {
// Do something
}

Robert Stam

unread,
Dec 13, 2010, 9:31:41 AM12/13/10
to mongodb-csharp
Thank you for reporting this issue. I have created a JIRA ticket for
this:

http://jira.mongodb.org/browse/CSHARP-134

You can track progress there.

rupurt

unread,
Dec 13, 2010, 8:17:49 PM12/13/10
to mongodb-csharp
No worries. I'm glad this will get fixed :) I've been trying to find a
solution for a week and i've only just found the google group.

Robert Stam

unread,
Dec 13, 2010, 8:27:06 PM12/13/10
to mongodb-csharp
It's fixed in the latest source code available on github if you're
willing to work with the bleeding edge.

Thanks for reporting the issue. Happy to help.
Reply all
Reply to author
Forward
0 new messages