Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3524

LINQ code generates incorrect $filter statement

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.14.0
    • Affects Version/s: 2.12.1
    • Component/s: Linq

      Hey guys,

      I found a problem with the following code:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      namespace MongoError
      {
          using System;
          using System.Collections.Generic;
          using System.Linq;
      
          using MongoDB.Bson;
          using MongoDB.Driver;
      
          public class Program
          {
              public static void Main(String[] args)
              {
                  var client = new MongoClient();
                  var database = client.GetDatabase("MyDB");
                  var collection = database.GetCollection<Item>("Items")
                                           .AsQueryable();
                  
                  var result = collection.SelectMany(x => x.Meta,
                                                     (item, meta) => new ProjectedItem
                                                     {
                                                         ItemId = item.Id.ToString(),
                                                         Meta = meta,
                                                         Property = item.Properties
                                                                        .First(p => p.Id == meta.PropertyId)
                                                     });
      
                  Console.WriteLine(result);
              }
      
              private class Item
              {
                  public ObjectId Id { get; set; }
                  public List<ItemMeta> Meta { get; set; }
                  public List<ItemProperty> Properties { get; set; }
              }
      
              private class ProjectedItem
              {
                  public String ItemId { get; set; }
                  public ItemMeta Meta { get; set; }
                  public ItemProperty Property { get; set; }
              }
      
              private class ItemMeta
              {
                  public String Meta { get; set; }
                  public Int32 PropertyId { get; set; }
              }
      
              private class ItemProperty
              {
                  public Int32 Id { get; set; }
                  public String Text { get; set; }
              }
          }
      }
      

      The generated aggregation pipeline looks like this:

      [
          {
              "$unwind": "$Meta"
          },
          {
              "$project": {
                  "ItemId": {
                      "$toString": "$_id"
                  },
                  "Meta": "$Meta",
                  "Property": {
                      "$arrayElemAt": [
                          {
                              "$filter": {
                                  "input": "$Properties",
                                  "as": "p",
                                  "cond": {
                                      "$eq": [
                                          "$$p._id",
                                          "$$p.Meta.PropertyId"
                                      ]
                                  }
                              }
                          },
                          0
                      ]
                  },
                  "_id": 0
              }
          }
      ]
      

      The condition part of the $filter statement is this:

      "$eq": [
          "$$p._id",
          "$$p.Meta.PropertyId"
      ]
      

      But I think it really should look like this:

      "$eq": [
          "$$p._id",
          "$Meta.PropertyId"
      ]
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            christian.flessa@gwvs.de Christian Flessa
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: