[CSHARP-1671] Using Contains with GroupBy and an Aggregate throws NotSupportedException Created: 18/May/16  Updated: 23/Sep/16  Resolved: 05/Aug/16

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.2.3
Fix Version/s: 2.3

Type: Bug Priority: Minor - P4
Reporter: Dave New Assignee: Craig Wilson
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The following query works:

return Database
    .GetCollection<MyEntity>()
    .AsQueryable()
    .Where(x => x.StartDate <= instance && x.EndDate >= instance)
    .GroupBy(x => x.Key.Guid)
    .Select(x => x.First().Id)
    .ToList();

But, when adding an $in condition (see below), the following exception is thrown:

An unhandled exception was thrown by the application. System.NotSupportedException: $project or $group does not support First({document}{_id})

return Database
    .GetCollection<MyEntity>()
    .AsQueryable()
    .Where(x => guidKeys.Contains(x.Key.Guid)) // $in condition
    .Where(x => x.StartDate <= instance && x.EndDate >= instance)
    .GroupBy(x => x.Key.Guid)
    .Select(x => x.First().Id)
    .ToList();

I understand that a lot of LINQ is not yet supported by the driver, but I can't understand how introducing that addition match stage (using $in) could result in an incompatibility for the grouping stage.

Is anyone able to explain why this is happening?

I am using MongoDB 3.2 with the .NET Driver 2.2.2.

MyEntity looks something like this:

[BsonIgnoreExtraElements]
public class MyEntity: BaseMongoDocument
{
    [BsonId]
    [BsonRepresentation(BsonType.Binary)]
    public Guid Id { get; set; }
 
    [BsonRequired]
    [BsonElement("startDate")]
    public DateTime StartDate { get; set; }
 
    [BsonRequired]
    [BsonElement("endDate")]
    public DateTime EndDate { get; set; }
 
    [BsonRequired]
    [BsonElement("key")]
    public SquidDocument Key { get; set; }
 
    [BsonConstructor]
    private MyEntity()
    { }
}
 
public class SquidDocument
{
    [BsonRequired]
    [BsonElement("guid")]
    public Guid Guid { get; private set; }
 
    [BsonRequired]
    [BsonElement("squid")]
    public string Squid { get; private set; }
 
    [BsonConstructor]
    private SquidDocument(Guid guid, string squid)
    {
        Guid = realSquid.Guid;
        Squid = realSquid.Value;
    }
}

Originally logged on StackOverflow: http://stackoverflow.com/questions/36838213/net-driver-with-linq-notsupportedexception-project-or-group



 Comments   
Comment by Githook User [ 05/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1671: fixes issue around groupings with a previous embedded pipeline.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/f139ddffd9874fd0fa304c2783f31816988d05ce

Comment by Omri Cohen [ 24/Jul/16 ]

+1
I'm facing the same issue, driver version 2.2.3
It's not the Key naming as suggested in the SO question, my property name is Id.
I can see 2.3 defined as the target version for this fix, do you have any date estimation?
Thanks!

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