MongoDB LinQ “Select” method will really retrieve only a subset of fields?

后端 未结 2 405
盖世英雄少女心
盖世英雄少女心 2021-01-06 19:01

Searching across the internet how to retrieve a subset of fields in MongoDB, using C# official driver (but using LinQ as the base architecture) I found how to do this in Mon

相关标签:
2条回答
  • 2021-01-06 19:21

    The driver does not currently retrieve a subset of the fields. If you need that functionality, you'll need to do it manually. The ticket for this functionality is here: https://jira.mongodb.org/browse/CSHARP-456. Feel free to leave feedback or vote it up if you need this.

    0 讨论(0)
  • 2021-01-06 19:35

    This is cheating... but:

    //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();
    
    0 讨论(0)
提交回复
热议问题