.Net Driver - Is there a way to cast IAggregateFluent to IQueryable?

1,471 views
Skip to first unread message

Josh Yaxley

unread,
Dec 9, 2016, 10:11:13 PM12/9/16
to mongodb-user
I find myself quite often wanting to perform an aggregation via .Net code, and then pass the results of this into something like Kendo.Dynamic.Linq's .ToDataSourceResult() method (http://www.telerik.com/blogs/kendo-ui-open-sources-dynamic-linq-helpers) which only takes an IQueryable as a parameter.

I know that when you cast an IMongoCollection to an IQueryable using .AsQueryable(), the LINQ commands get converted into aggregation stages. It seems reasonable to me that an IAggregateFluent (which is composed of aggregation stages), can be converted to an IQueryable (which is also composed of aggregation stages).

I've looked into this a lot, and cannot find any way to do it.

Is it something that anyone else would find useful? Is it something that might make it into a future release?

I thought Views might be a partial solution to this, because you can read them in as IMongoCollections and then cast them to IQueryables, but the problem there is that you cannot control any of the view-defined aggregation stages (for example, if I want to filter to records of a particular user, I have to append this to the end of the aggregation, as this dynamic data cannot be included in the view-defined aggregation - this resulted in a full collection scan which did not give good enough performance!)

Wan Bachtiar

unread,
Dec 19, 2016, 2:24:40 AM12/19/16
to mongodb-user

It seems reasonable to me that an IAggregateFluent (which is composed of aggregation stages), can be converted to an IQueryable (which is also composed of aggregation stages).

Hi Josh,

This is because an aggregation pipeline can be applied to IMongoCollection i.e. a kin to collection.aggregate([]) . An aggregation pipeline however, returns an aggregated result which are not persistent in the database. With an exception being if you were to specify $out operator to write into a specified collection. You can then query this collection using IQueryable.

Note that $out stage atomically replaces an existing collection with the same name. See also $out behaviours.

for example, if I want to filter to records of a particular user, I have to append this to the end of the aggregation, as this dynamic data cannot be included in the view-defined aggregation - this resulted in a full collection scan which did not give good enough performance!)

I assumed you’re referring to MongoDB Views on v3.4. Note that views use indexes of the underlying collection see views behaviour. See also Aggregation pipeline optimisation.

If you have further questions, could you provide:

  • MongoDB .Net/C# driver version
  • MongoDB version
  • An example code snippet and sample documents for reference.

Regards,

Wan.

Robert Stam

unread,
Dec 21, 2016, 3:56:59 PM12/21/16
to mongod...@googlegroups.com
It is not possible to convert an instance of an IAggregateFluent to an instance of an IQueryable.

You are right that they both eventually become aggregate pipelines sent to the server with the aggregate command, but internally they are quite different and not easily convertible to each other.


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/5dde6ad9-e3da-409c-9888-261833594f8c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages