mongodb-csharp驱动程序 - 如何将属性保存为引用而不是嵌入?

时间:2010-10-04 16:30:45

标签: mongodb mongodb-.net-driver

对于我们的C#.NET项目,我们在Mongo DB上做了一点点高峰,看看它是否适合我们,而且我遇到了samus的mongodb-csharp驱动程序的一个小问题,我不确定如何实施。

鉴于以下简化模型:

public class Campaign
{        
    public string Name { get; set; }
    public IEnumerable<Placement> Placements { get; set; }
}

public class Placement
{
    public string Name { get; set; }

    //this should be a reference rather than included in the collection
    public Site Site { get; set; }
}

//this should be its own collection, and not embedded anywhere
public class Site
{
    public string Name { get; set; }
}

正如上面的评论所示,我们试图找出如何将Site保存为引用类型而不是嵌入Placement中。网站是其自己的顶级集合,可以独立于每个广告系列进行更改。

理想情况下,我想使用MongoConfigurationBuilder执行此操作,而不必修改我的POCO。我似乎无法找到有关如何实现这一目标的任何文档。

我希望它会像以下一样简单:

var config = new MongoConfigurationBuilder();

config.Mapping(mapping =>
{
    //maybe some more configuration here?
    mapping.Map<Site>();
    mapping.Map<Campaign>();
});

但是当我使用以下代码时,它仍然嵌入了网站:

var db = mongo.GetDatabase("foo");

var campaignCollection = db.GetCollection<Campaign>();
var siteCollection = db.GetCollection<Site>();

var firstSite = new Site{Name = "first site"};
var secondSite = new Site{Name = "second site"};

var firstCampaign = new Campaign
{
    Name = "first campaign",
    Placements = new List<Placement>
    {
        new Placement{Name = "first placement", Site = firstSite},
        new Placement{Name = "second placement", Site = secondSite}
    }
};

siteCollection.Save(firstSite);
siteCollection.Save(secondSite);

campaignCollection.Save(firstCampaign);

这给了我们:

{ "_id" : ObjectId("4ca9f1db54730000000010cb"), 
  "Name" : "first campaign", 
  "Placements" : [
    {
      "Name" : "first placement",
      "Site" : { "Name" : "first site" }
    },
    {
      "Name" : "second placement",
      "Site" : { "Name" : "second site" }
    }
  ]}

{ "_id" : ObjectId("4ca9f1db54730000000010c9"), "Name" : "first site" }
{ "_id" : ObjectId("4ca9f1db54730000000010ca"), "Name" : "second site" }

我们想要更像的东西:

{ "_id" : ObjectId("4ca9f1db54730000000010cb"), 
  "Name" : "first campaign", 
  "Placements" : [
    {
      "Name" : "first placement",
      "Site" : ObjectId("4ca9f1db54730000000010c9")
    },
    {
      "Name" : "second placement",
      "Site" : ObjectId("4ca9f1db54730000000010ca")
    }
  ]}

{ "_id" : ObjectId("4ca9f1db54730000000010c9"), "Name" : "first site" }
{ "_id" : ObjectId("4ca9f1db54730000000010ca"), "Name" : "second site" }

我不确定这是否是我们最终得到的确切文件,但你明白了。

我觉得好像我错过了一些明显的东西,但如果没有关于驾驶员这方面的更好的文档,我就是在黑暗中拍摄。我甚至查看了源代码中的测试,但无法弄明白。

有谁知道怎么做?有可能吗?

1 个答案:

答案 0 :(得分:9)

为此,您必须使用DBRef

要在c#中执行此操作,您必须像这样更改广告系列类

public class Campaign
{        
    public string Name { get; set; }
    public List<DBRef> Placements { get; set; }
}

并插入像这样的参考文件

List<DBRef> refList = new List<DBRef>() {firstSite,secondSite };


var firstCampaign = new Campaign
{
    Name = "first campaign",
    Placements = refList
};

campaignCollection.Save(firstCampaign);

我还没有测试过代码,但是这给出了如何实现这一目的的提示。