Mongo DB C#驱动程序-如何在集合中通过ID加入? [英] Mongo db c# driver - how to join by id in collection?

查看:95
本文介绍了Mongo DB C#驱动程序-如何在集合中通过ID加入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mongo DB c#驱动程序2, 我试图通过ID(多对多)加入2个收藏夹

I'm using Mongo DB c# driver 2, I'm trying to join 2 collections by ID (Many to Many)

Class A
{
   public string id;
   public string name;
   public list<string> classBReferenceid; // <--- I want use this as the "keys" for the join
   public list<B> myBs;
}
Class B
{
   public string id; // <--- I use this as the "key" for the join
   public string name;
}

在我的数据库类中,"A"被保存而没有"myBs"数据,我想在一次调用中将其从mongo中拉出.

In my DB class "A" is saved without the data of "myBs" and I want to pull it from mongo in one call.

我尝试使用Lookup函数:

I tried to use the Lookup function:

IMongoCollection<A> mongoACollection = // already set in driver....
IMongoCollection<B> mongoBCollection = // already set in driver....

IAggregateFluent<A> results = _mongoACollection.Aggregate().
                Lookup<A, B, A>(
                    mongoBCollection,
                    a => a.classBReferenceid,
                    b => b.Id,
                    a => a.myBs);

但是它不起作用(不加入任何内容),可能是因为"classBReference"是列表而不是"id".

But it doesn't work (doesn't join anything) probably because the "classBReference" is a list and not an "id".

如何使用查找来按ID加入出现在另一个集合的ID列表中的集合?

推荐答案

此功能在MongoDb v3.3.4中引入,但是我的本地实例通过Mongo2Go使用了MongoDb 3.2,我将其用于单元测试.

This feature was introduced in MongoDb v3.3.4, but my local instance was using MongoDb 3.2 via Mongo2Go, which I use for unit tests.

一旦我将本地实例升级到v3.3.4,问题就解决了.

Once I upgraded my local instance to v3.3.4, the issue was solved.

这篇关于Mongo DB C#驱动程序-如何在集合中通过ID加入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆