System.NotSupportedException: '$project 或 $group 不支持 {document}.'MongoDB [英] System.NotSupportedException: '$project or $group does not support {document}.' mongodb

查看:132
本文介绍了System.NotSupportedException: '$project 或 $group 不支持 {document}.'MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 select 中执行 join 和 filter 时,mongodb 2.4.4 驱动器抛出此异常:

When I try to execute join and filter in a select, mongodb 2.4.4 drive throws this exception:

System.NotSupportedException: '$project 或 $group 不支持{文档}.'mongodb

System.NotSupportedException: '$project or $group does not support {document}.' mongodb

以下加入和过滤有什么问题?

What is issue with below join and filter?

var appt = (from col1 in collection1.AsQueryable()
    join col2 in collection2.AsQueryable() on col1.Id equals col2.RefKey into grp
    select new {
        col1, filteredCol = col1.BandId == "" ? grp : grp.Where(t => t.Name ="Hello")
    }).Take(10).ToList();

推荐答案

您的问题不是由加入或过滤引起的,而是由您的投影引起的.投影不应该包括文档本身,这只是 MongoDB .Net 驱动程序不支持.

Your problem is caused not by join or filter, but by your projection. Projection should not include document itself, this just is not supported by MongoDB .Net driver.

因此,以下查询在投影中不包含 col1 对象的情况下也能正常工作:

So the following query without including col1 object in the projection will work just fine:

var appt = (from col1 in collection1.AsQueryable()
    join col2 in collection2.AsQueryable() on col1.Id equals col2.RefKey into grp
    select new
    {
        //col1,
        filteredCol = col1.BandId == "" ? grp : grp.Where(t => t.Name == "Hello")
    }).Take(10).ToList();

这里有几个可能的修复方法.最好的选择是在投影中不包括整个文档,而只包括进一步逻辑实际需要的字段,例如:

There are several possible fixes here. The best choice is to include in projection not the whole document but only the fields that are actually required for further logic, e.g.:

var appt = (from col1 in collection1.AsQueryable()
    join col2 in collection2.AsQueryable() on col1.Id equals col2.RefKey into grp
    select new
    {
        col1.Id,
        col1.BandId,
        filteredCol = col1.BandId == "" ? grp : grp.Where(t => t.Name == "Hello")
    }).Take(10).ToList();

但是,如果您需要文档对象本身,则可以使用 .AsEnumerable() 将整个集合提取到客户端,然后使用 LINQ to objects:

If however you need the document object itself, you could fetch the whole collection to the client with .AsEnumerable() and then use LINQ to objects:

var appt = (from col1 in collection1.AsQueryable().AsEnumerable()
    join col2 in collection2.AsQueryable() on col1.Id equals col2.RefKey into grp
    select new
    {
        col1,
        filteredCol = col1.BandId == "" ? grp : grp.Where(t => t.Name == "Hello")
    }).Take(10).ToList();

将此方法用作最后一个选项,因为它会同时负载大量服务器和客户端.

Use this approach as last option because it loads heavily both server and client.

这篇关于System.NotSupportedException: '$project 或 $group 不支持 {document}.'MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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