Reputation: 721
Hi I am new to MongoDB and ASP.Net. I was wondering if MongoDB projection retrieves the fields from a document in the server itself or does it retrieve the entire document and filter it in memory.
For Example:
var filter = Builders<FoodItems>.Filter.Where(r => r.Fruits.Name == "Mango");
var result = Context.FruitCollection
.Find(filter)
.Project(r => new {r.Fruits.Cost, r.Fruits.Quantity})
.ToList();
return result;
Here, the "Cost" and "Quantity" fields are retrieved from the database directly or the entire "FoodItems" Document is retrieved and the corresponding fields are retrieved from the memory?
Thanks in advance.
Upvotes: 2
Views: 319
Reputation: 9208
Projection happens on the database server side; it is typically used to limit the amount of data that MongoDB sends to applications.
One particular example that shows how MongoDB makes use of projection for optimising its own running is when all the fields to be returned from a query are included in the index used to find the data; that is called a covered query, and it means that once MongoDB has found the index entries it can finish immediately, without having to go further to the actual collection data.
Upvotes: 3