无法使用 MongoDBRef 枚举对象

发布于 2024-10-06 10:30:33 字数 1209 浏览 9 评论 0原文

我有一个名为 Products 的集合,我试图使用官方 mongo-csharp 驱动程序来枚举它。然而,一旦我尝试枚举集合(例如使用 foreach 循环),我就会收到以下错误。

“未找到 MongoDB.Driver.MongoDBRef 类型的默认构造函数”

实体类如下所示

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; }
}

集合中的条目如下所示

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

我正在像这样读取它。

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

在执行以下任一操作之前,我不会收到错误消息。

var l = matches.ToList();

或者

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

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/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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国际总奸 2024-10-13 10:30:33
  1. 使用 mongovue 连接到 mongo db 并检查集合和数据是否存在。
  2. 显示代码

    var db = Db.Instance.GetDatabase();

应该是这样的:

var server = MongoServer.Create("mongodb://localhost:27019");
var db =  server.GetDatabase("database_name");

比你的代码:

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

3.我检查了c#的mongo驱动程序的来源,我在MongoDBRef中发现了以下内容

 // default constructor is private and only used for deserialization
    private MongoDBRef() {
    } 

所以我建议在你版本的mongo驱动程序中,来自mongo c#驱动程序团队的人忘记了默认构造函数。请以任何方式使用 reflector 检查构造函数是否存在。
4. 我 99% 确信您的 mongo 驱动程序版本中不存在该构造函数。因为当您开始枚举一些 mongo 集合时,mongo 驱动程序将需要数据,并且如果找不到默认构造函数,则会抛出错误。

  1. Connect to mongo db using mongovue and check that collection and data exists.
  2. Show code of

    var db = Db.Instance.GetDatabase();

Should be something like this:

var server = MongoServer.Create("mongodb://localhost:27019");
var db =  server.GetDatabase("database_name");

and than yous code:

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

3.I've checked source of mongo driver for c# and i found following in MongoDBRef

 // default constructor is private and only used for deserialization
    private MongoDBRef() {
    } 

So i suggest that in you version of mongo driver guys from mongo c# driver team forgot about default constructor. In any way check please that constructor exist/not exist using reflector.
4. And i am 99% sure that constructor not present at yous version of mongo driver. Because when you start to enumerate some mongo collection mongo driver going to desirealize data and in case if default constructor not found throws error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文