Projecting with AsQueryable in MongoDB C # Driver 2.2

I am trying to use the MongoDB C # driver version 2.2. I am trying to use projection because I do not want to extract all the elements in the document. I found one way to do this, to use the project statement along with the find statement, something like this:

collection.Find(key => key.Index == 1).Project<MyClass>(Builders<MyClass>.Projection.Include(key => key.Name).Include(key => key.Index)). ToEnumerable ();

However, I am interested in using the AsQueryable API with a statement, something like this:

collection.AsQueryable().Where(key => key.Index == 1);

Can projection be used in this case? If I use the select statement, will it have the same effect as projection? Or will they still retrieve all the items from the database server and then select specific items on the application server?

+4
source share
1

, . Select (Select(i => new { i.Name, i.Index})) ToString , , Linq (a $match a $project),

var query=collection.AsQueryable().Where(key => key.Index == 1).Select(k=>new {k.Name,k.Index});
var aggregate= query.ToString();

, , a Select $project.

, , ToList ( ) .

+5

Source: https://habr.com/ru/post/1627473/


All Articles