public static IAggregateFluent <TNewResult> Unwind <TResult, TNewResult>(this IAggregateFluent <TResult> aggregate, Expression <Func <TResult, object> > field, IBsonSerializer <TNewResult> newResultSerializer)
 {
     Ensure.IsNotNull(aggregate, nameof(aggregate));
     return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Unwind(field, new AggregateUnwindOptions <TNewResult> {
         ResultSerializer = newResultSerializer
     })));
 }
Esempio n. 2
0
        public void Facets()
        {
            var collection = new MongoClient("mongodb://localhost:27017/admin").GetDatabase("firstdb").GetCollection <Book>("book");
            var car1       = new Book()
            {
                Name = "b1",
                Tags = new System.Collections.Generic.Dictionary <string, string>()
                {
                    { "Edition", "Blah" },
                    { "Published", "2018" }
                }
            };

            collection.InsertOne(car1);

            var pipelinex = collection.Aggregate()
                            .Match(b => b.Name == "b1")
                            .Project("{Tags: { $objectToArray: \"$Tags\" }}")
                            .Unwind("Tags")
                            .SortByCount <BsonDocument>("$Tags");

            var outputx = pipelinex.ToList();
            var json    = outputx.ToJson(new JsonWriterSettings {
                Indent = true
            });


            var project     = PipelineStageDefinitionBuilder.Project <Book, BsonDocument>("{Tags: { $objectToArray: \"$Tags\" }}");
            var unwind      = PipelineStageDefinitionBuilder.Unwind <BsonDocument, BsonDocument>("Tags");
            var sortByCount = PipelineStageDefinitionBuilder.SortByCount <BsonDocument, BsonDocument>("$Tags");

            var pipeline = PipelineDefinition <Book, AggregateSortByCountResult <BsonDocument> > .Create(new IPipelineStageDefinition[] { project, unwind, sortByCount });

            // string based alternative version
            //var pipeline = PipelineDefinition<Book, BsonDocument>.Create(
            //    "{ $project :{ Tags: { $objectToArray: \"$Tags\" } } }",
            //    "{ $unwind : \"$Tags\" }",
            //    "{ $sortByCount : \"$Tags\" }");

            var facetPipeline = AggregateFacet.Create("categorizedByTags", pipeline);

            var aggregation = collection.Aggregate().Match(b => b.Name == "b1").Facet(facetPipeline);

            var listx = aggregation.ToList();
            // var outputxy = listx.Facets.ToJson(new JsonWriterSettings { Indent = true });
            var output = aggregation.Single().Facets.ToJson(new JsonWriterSettings {
                Indent = true
            });

            Console.WriteLine(output);
        }
 /// <summary>
 /// Appends an unwind stage to the pipeline.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <typeparam name="TNewResult">The type of the new result.</typeparam>
 /// <param name="aggregate">The aggregate.</param>
 /// <param name="field">The field to unwind.</param>
 /// <param name="options">The options.</param>
 /// <returns>
 /// The fluent aggregate interface.
 /// </returns>
 public static IAggregateFluent <TNewResult> Unwind <TResult, TNewResult>(this IAggregateFluent <TResult> aggregate, Expression <Func <TResult, object> > field, AggregateUnwindOptions <TNewResult> options = null)
 {
     Ensure.IsNotNull(aggregate, nameof(aggregate));
     return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Unwind(field, options)));
 }
 /// <summary>
 /// Appends an unwind stage to the pipeline.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="aggregate">The aggregate.</param>
 /// <param name="field">The field to unwind.</param>
 /// <returns>
 /// The fluent aggregate interface.
 /// </returns>
 public static IAggregateFluent <BsonDocument> Unwind <TResult>(this IAggregateFluent <TResult> aggregate, Expression <Func <TResult, object> > field)
 {
     Ensure.IsNotNull(aggregate, nameof(aggregate));
     return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Unwind(field)));
 }
Esempio n. 5
0
        private IAggregateFluent <ClothKind> GetAggregationFluentForAggregation(bool includeDeleted = false,
                                                                                FilterDefinition <ClothKind> filter = null, FilterDefinition <Order> filterOrder = null)
        {
            var instancesProjectDef = @"{
  ClothKind : '$Instances.ClothKind',
  Amount: '$Instances.Amount'
}";

            PipelineDefinition <Order, BsonDocument> innerpipeline = PipelineDefinition <Order, BsonDocument>
                                                                     .Create(new IPipelineStageDefinition[]
            {
                PipelineStageDefinitionBuilder.Match(filterOrder ?? Builders <Order> .Filter.Empty),
                PipelineStageDefinitionBuilder.Unwind <Order>("Instances"),
                PipelineStageDefinitionBuilder.Project <BsonDocument>(instancesProjectDef),
                PipelineStageDefinitionBuilder.Match <BsonDocument>(new BsonDocument("$expr",
                                                                                     new BsonDocument("$eq",
                                                                                                      new BsonArray
                {
                    "$ClothKind",
                    "$$kindid"
                }))),
            });

            var groupDef = @"{
  _id: ""$_id"",
  Name :{ $first: ""$Name""},
  MeasureKind: { $first:""$MeasureKind""},
  Price :{ $first: ""$Price""},
  Parent:{$first:""$Parent""},
  Count: {
    $sum:""$ClothInstances.Amount""
  },
  SumPrice:{
    $sum: {$multiply : [""$Price"", ""$ClothInstances.Amount""]}
  }
}";

            var projectDef = @"{
  Name: '$Name',
  MeasureKind: '$MeasureKind',
  Price : '$Price',
  Parent : '$Parent',
  Count : '$Count',
  SumPrice: '$SumPrice',
  ChildrenCount : {$size: '$Children'} 
}";

            var aggregateUnwindOptions = new AggregateUnwindOptions <BsonDocument> {
                PreserveNullAndEmptyArrays = true
            };


            return(base.GetAggregationFluent(includeDeleted, filter)
                   .Lookup <Order, BsonDocument, IList <BsonDocument>, ClothKind>(
                       this.Collection.Database.GetCollection <Order>("orders"),
                       new BsonDocument("kindid", "$_id"), innerpipeline, "ClothInstances")
                   .Unwind("ClothInstances", aggregateUnwindOptions)
                   .Group(groupDef)
                   .Lookup("clothkinds", "_id", "Parent", "Children")
                   .Project(projectDef)
                   .As <ClothKind>()
                   .SortBy(x => x.Id));
        }