$ project or $ group does not support <document>
I try to start an aggregate with a projection, but I get a NotSupportedException: $project or $group does not support <document> . I am running version 2.4.4 of the driver with mongodb v3.4.
var filter = Builders<T>.Filter.Regex(x=>x.Value,"/test/gi"); var aggregate = collection.Aggregate() .Match(filter) .Project(x => new { Idx = x.Value.IndexOf("test"), Result = x }) .SortBy(x => x.Idx); I thought IndexOfCP supported .
What am I doing wrong here?
The problem is not caused by IndexOf , but by your projection. Projection should not include the document itself, it is simply not supported by the MongoDB.Net driver. Thus, the following query without including the object x in the projection will work fine:
var aggregate = collection.Aggregate() .Match(filter) .Project(x => new { Idx = x.Value.IndexOf("test"), // Result = x }) .SortBy(x => x.Idx); There are several possible fixes. The best choice is to include in the projection not the entire document, but only those fields that are really necessary for further logic, for example:
var aggregate = collection.Aggregate() .Match(filter) .Project(x => new { Idx = x.Value.IndexOf("test"), Value = x.Value, // ... }) .SortBy(x => x.Idx); If you need the document object itself, you can get the entire collection for the client, and then use LINQ for the objects:
var aggregate = collection.Aggregate() .Match(filter) .ToList() .Select(x => new { Idx = x.Value.IndexOf("test"), Result = x }) .OrderBy(x => x.Idx); Use this approach as the last parameter, as it loads the server and client heavily.