【问题标题】:Get all Documents from one Collection that are not referenced from another collection从一个集合中获取未从另一个集合引用的所有文档
【发布时间】:2018-03-10 14:59:34
【问题描述】:

我正在尝试从集合 A 中查找未从集合 B 中的任何文档中引用的所有文档。

我找到了this answer,但我无法将其翻译成 C#。

到目前为止,我已经尝试过:

from a in docA.AsQueryable()
join b in docB.AsQueryable() on a.Id equals b.DocARef into bs
from sub_b in bs.DefaultIfEmpty()
where sub_b == null
select new { a.Id, a.Name };

from a in docA.AsQueryable()
join b in docB.AsQueryable() on a.Id equals b.DocARef into bs
from sub_b in bs.DefaultIfEmpty()
where !bs.Any()
select new { a.Id, a.Name };

两者都导致NotSupportedException$project or $group does not support {document}.

我做错了什么?

【问题讨论】:

    标签: c# mongodb mongodb-.net-driver


    【解决方案1】:

    这里的 C# 驱动程序似乎没有正确遵循 LINQ 的约定。

    正确的查询如下所示:

    var result =
    from a in docA.AsQueryable()
    join b in docB.AsQueryable() on a.Id equals b.DocARef into bs
    where !bs.Any()
    select new { a.Id, a.Name };
    

    请注意,没有DefaultIfEmpty,但连接的行为仍然类似于左外连接。

    【讨论】:

      猜你喜欢
      • 2019-02-05
      • 1970-01-01
      • 2018-04-20
      • 2020-05-30
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2021-10-30
      相关资源
      最近更新 更多