Mongodb driver linq query converting to aggregate command instead of find

Smith
Smith
None
2568 Points
74 Posts

I'm using MongoDb .net driver to do linq query to db and working fine. But when doing load testing and checking the actual command execution in db then seems event simple linq query converting to aggregate and degrading performance.

Following is linq query:

var order = await mongoDbService.GetCollection<MyOrders>(COLLECTION_NAME)
                        .AsQueryable()
                        .FirstOrDefaultAsync(x => x.orderId == orderId);

It converting to aggregate command like:

{ "aggregate" : "my-orders", "pipeline" : [{ "$match" : { "orderId" : "456241" } },

But I want simple find command instead of aggregate and pipeline something like:

{ "find" : "my-orders", "filter" : { "orderId" : "456241" }, ...}

Is there any work-around for this?

Views: 1099
Total Answered: 2
Total Marked As Answer: 0
Posted On: 30-Dec-2021 06:08

Share:   fb twitter linkedin
Answers
beginer
beginer
Member
1328 Points
43 Posts
         

Yes, you are right as per mongodb documentation Linq intended to use Aggregate framework, here we can refer : https://mongodb.github.io/mongo-csharp-driver/2.11/reference/driver/crud/linq/

"The driver contains an implementation of LINQ that targets the aggregation framework. The aggregation framework holds a rich query language that maps very easily from a LINQ expression tree making it straightforward to understand the translation from a LINQ statement into an aggregation framework pipeline."

Posted On: 31-Dec-2021 05:54
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

I think, there is way to map to find query. For this we can use fluent find interface on MongoDb driver:

And we can use it as:

var order = await mongoDbService.GetCollection<MyOrders>(COLLECTION_NAME)
                        .Find(x => x.orderId == orderId).FirstOrDefaultAsync();

 

Posted On: 04-Jan-2022 22:10
 Log In to Chat