[CSHARP-2104] $project or $group does not support <document> Created: 20/Nov/17  Updated: 14/Jun/19  Resolved: 12/Feb/19

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.4
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: rethabile Assignee: Wan Bachtiar
Resolution: Duplicate Votes: 1
Labels: wb
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows 10, asp.net core


Issue Links:
Duplicate
duplicates CSHARP-2071 Using Linq Aggregation to project the... Closed
Related
related to CSHARP-2071 Using Linq Aggregation to project the... Closed

 Description   

Originally on SO.

I'm trying to run aggregate with projection but i get NotSupportedException: $project or $group does not support <document>. I am running version 2.4.4 of 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 is supported.

What am i doing wrong here?



 Comments   
Comment by Wan Bachtiar [ 12/Feb/19 ]

i get NotSupportedException: $project or $group does not support document

Hi,

As pointed out in your StackOverflow post, the issue here is caused by trying to project the whole document. You can actually project only specific fields within the document. i.e. Result = x.foo.
As of current version of the driver, the projection of $$ROOT  (whole document) is not supported. See CSHARP-2071 for the feature request ticket.

the use case is just search where the most matching (by Idx) are placed at the top, so the Idx is just for sort

Based on your comment on StackOverflow post, it seems that your use case is trying to search and sort by the most relevant matching. 
If so, you can utilise $text operator to perform text search and outputs a meta score based on the matching. For example:

var filter = Builders<Test>.Filter.Text("test");
var projection = Builders<Test>.Projection.MetaTextScore("score").Include("Value");
var sort = Builders<BsonDocument>.Sort.MetaTextScore("score");
var aggregate = collection.Aggregate()
                .Match(filter)
                .Project(projection)
                .Sort(sort);

You may also find Text Search in Aggregation Pipeline useful.

Regards,
Wan.

Generated at Wed Feb 07 21:41:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.