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

Using Linq Aggregation to project the root document

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

      Expected Behaviour

      Using the aggregation pipeline I can perform the following:

      • project an entire document using the $$ROOT variable
      • perform some filtering
      • then using ReplaceRoot return the same structure

      Example below:

      db.test.aggregate(
      	[
      		{
      			$project: {
      			    "doc": '$$ROOT', "dif":{"$setDifference":["$X",["tag1","tag2"]]},"_id":0
      			}
      		},
      		{
      			$match: {
      				"dif":{"$size":0}
      			}
      		},
      		{
      			$replaceRoot: {
      			    newRoot: '$doc'
      			}
      		},
      	]
      );
      

      Using Linq, I believe this would be represented using the following Linq query:

      collection.AsQueryable()
      		.Select(c => new { doc = c, dif = c.X.Except(tags), Id = c.Id })
      		.Where(c => c.dif.Count() == 0)
      		.Select(c => c);
      

      Actual Behaviour

      Returns the following error when executed:

      $project or $group does not support {document}.

      Stack trace:

         at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
         at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMapping(ProjectionMapping mapping)
         at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateProjectValue(Expression selector)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSelect(SelectExpression node)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node)
         at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
         at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Translate(Expression expression)
         at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)
         at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()
         at MongoDB.Driver.Linq.MongoQueryableImpl`2.System.Collections.IEnumerable.GetEnumerator()
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            stephencbond@gmail.com Stephen Bond
            Votes:
            6 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: