C# MongoDB LinQ“Select”方法真的只检索字段的子集吗?
通过internet搜索如何在MongoDB中检索字段子集,使用C官方驱动程序,但使用LinQ作为基本架构,我在MongoDB shell中找到了如何实现这一点C# MongoDB LinQ“Select”方法真的只检索字段的子集吗?,c#,performance,mongodb,mongodb-.net-driver,subset,C#,Performance,Mongodb,Mongodb .net Driver,Subset,通过internet搜索如何在MongoDB中检索字段子集,使用C官方驱动程序,但使用LinQ作为基本架构,我在MongoDB shell中找到了如何实现这一点 // selecting only "field" of a collection db.collection.find( { field : 'value' }, { field: 1 } ); 然后,我在C LinQ教程中找到了Select方法,它等效于: collection.AsQueryable<T>().Se
// selecting only "field" of a collection
db.collection.find( { field : 'value' }, { field: 1 } );
然后,我在C LinQ教程中找到了Select方法,它等效于:
collection.AsQueryable<T>().Select(x => new { x.field });
但是,教程说,该方法用于从匹配文档中投影新的结果类型
如何确保此方法仅检索字段子集而不是整个结果,然后仅将子集选择到新对象中
驱动程序会在检索结果之前生成查询命令吗?这是作弊。。。但是:
//This actual implementation is untested and may contain small errors.
//The helper method has been tested and *should* work.
public static IMongoQuery GetMongoQuery<T>(this IQueryable<T> query)
{
return ((MongoQueryable<T>)query).GetMongoQuery();
}
var temp =
from x in DB.Foo.AsQueryable<Test>()
where x.SomeField > 5;
select (x.OtherField);
return temp.GetMongoQuery().ToJson();
驱动程序当前未检索字段的子集。如果您需要该功能,则需要手动完成。此功能的票证位于此处:。如果需要,请随时留下反馈或投票