public void Lookup_with_entity_generic_params_should_return_the_expected_result() { RequireServer.Check().Supports(Feature.AggregateLet); var client = new MongoClient(CoreTestConfiguration.ConnectionString.ToString()); var warehousesCollection = client.GetDatabase("test").GetCollection <Warehouse>("warehouses"); var lookupPipeline = new EmptyPipelineDefinition <Warehouse>() .Match(new BsonDocument("$expr", new BsonDocument("$and", new BsonArray { new BsonDocument("$eq", new BsonArray { "$stock_item", "$$order_item" }), new BsonDocument("$gte", new BsonArray { "$instock", "$$order_qty" }) }))) .Project <Warehouse, Warehouse, StockData>( Builders <Warehouse> .Projection .Exclude(warehouses => warehouses.StockItem)); var result = PipelineStageDefinitionBuilder.Lookup <BsonDocument, Warehouse, StockData, IEnumerable <StockData>, Order>( warehousesCollection, new BsonDocument { { "order_item", "$item" }, { "order_qty", "$ordered" } }, lookupPipeline, order => order.StockData ); RenderStage(result).Document.Should().Be(@" { '$lookup' : { 'from' : 'warehouses', 'let' : { 'order_item' : '$item', 'order_qty' : '$ordered' }, 'pipeline' : [ { '$match' : { '$expr' : { '$and' : [ { '$eq' : ['$stock_item', '$$order_item'] }, { '$gte' : ['$instock', '$$order_qty'] }] } } }, { '$project' : { 'stock_item' : 0 } }], 'as' : 'stockdata' } }"); }
public void Lookup_without_optional_let_should_return_the_expected_result() { RequireServer.Check().Supports(Feature.AggregateLet); var client = new MongoClient(CoreTestConfiguration.ConnectionString.ToString()); var warehousesCollection = client.GetDatabase("test").GetCollection <BsonDocument>("warehouses"); var lookupPipeline = new EmptyPipelineDefinition <BsonDocument>(); var result = PipelineStageDefinitionBuilder.Lookup <BsonDocument, BsonDocument, BsonDocument, IEnumerable <BsonDocument>, BsonDocument>( warehousesCollection, null, lookupPipeline, new StringFieldDefinition <BsonDocument, IEnumerable <BsonDocument> >("stockdata") ); RenderStage(result).Document.Should().Be(@" { '$lookup' : { 'from' : 'warehouses', 'pipeline' : [ ], 'as' : 'stockdata' } }"); }
/// <summary> /// Appends a lookup stage to the pipeline. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <typeparam name="TForeignDocument">The type of the foreign collection.</typeparam> /// <typeparam name="TNewResult">The type of the new result.</typeparam> /// <param name="aggregate">The aggregate.</param> /// <param name="foreignCollection">The foreign collection.</param> /// <param name="localField">The local field.</param> /// <param name="foreignField">The foreign field.</param> /// <param name="as">The field in the result to place the foreign matches.</param> /// <param name="options">The options.</param> /// <returns>The fluent aggregate interface.</returns> public static IAggregateFluent <TNewResult> Lookup <TResult, TForeignDocument, TNewResult>( this IAggregateFluent <TResult> aggregate, IMongoCollection <TForeignDocument> foreignCollection, Expression <Func <TResult, object> > localField, Expression <Func <TForeignDocument, object> > foreignField, Expression <Func <TNewResult, object> > @as, AggregateLookupOptions <TForeignDocument, TNewResult> options = null) { Ensure.IsNotNull(aggregate, nameof(aggregate)); return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Lookup(foreignCollection, localField, foreignField, @as, options))); }
/// <summary> /// Appends a lookup stage to the pipeline. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="aggregate">The aggregate.</param> /// <param name="foreignCollectionName">Name of the foreign collection.</param> /// <param name="localField">The local field.</param> /// <param name="foreignField">The foreign field.</param> /// <param name="as">The field in the result to place the foreign matches.</param> /// <returns>The fluent aggregate interface.</returns> public static IAggregateFluent <BsonDocument> Lookup <TResult>( this IAggregateFluent <TResult> aggregate, string foreignCollectionName, FieldDefinition <TResult> localField, FieldDefinition <BsonDocument> foreignField, FieldDefinition <BsonDocument> @as) { Ensure.IsNotNull(aggregate, nameof(aggregate)); Ensure.IsNotNull(foreignCollectionName, nameof(foreignCollectionName)); var foreignCollection = aggregate.Database.GetCollection <BsonDocument>(foreignCollectionName); return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Lookup(foreignCollection, localField, foreignField, @as))); }
/// <summary> /// Appends a lookup stage to the pipeline. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="aggregate">The aggregate.</param> /// <param name="foreignCollection">The foreign collection.</param> /// <param name="let">The "let" definition.</param> /// <param name="lookupPipeline">The lookup pipeline.</param> /// <param name="as">The as field in the result in which to place the results of the lookup pipeline.</param> /// <returns>The fluent aggregate interface.</returns> public static IAggregateFluent <BsonDocument> Lookup <TResult>( this IAggregateFluent <TResult> aggregate, IMongoCollection <BsonDocument> foreignCollection, BsonDocument let, PipelineDefinition <BsonDocument, BsonDocument> lookupPipeline, FieldDefinition <BsonDocument, IEnumerable <BsonDocument> > @as) { Ensure.IsNotNull(aggregate, nameof(aggregate)); Ensure.IsNotNull(foreignCollection, nameof(foreignCollection)); return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Lookup <TResult, BsonDocument, BsonDocument, IEnumerable <BsonDocument>, BsonDocument>( foreignCollection, let, lookupPipeline, @as))); }
/// <summary> /// Appends a lookup stage to the pipeline. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <typeparam name="TForeignDocument">The type of the foreign collection documents.</typeparam> /// <typeparam name="TAsElement">The type of the as field elements.</typeparam> /// <typeparam name="TAs">The type of the as field.</typeparam> /// <typeparam name="TNewResult">The type of the new result.</typeparam> /// <param name="aggregate">The aggregate.</param> /// <param name="foreignCollection">The foreign collection.</param> /// <param name="let">The "let" definition.</param> /// <param name="lookupPipeline">The lookup pipeline.</param> /// <param name="as">The as field in <typeparamref name="TNewResult" /> in which to place the results of the lookup pipeline.</param> /// <param name="options">The options.</param> /// <returns>The fluent aggregate interface.</returns> public static IAggregateFluent <TNewResult> Lookup <TResult, TForeignDocument, TAsElement, TAs, TNewResult>( this IAggregateFluent <TResult> aggregate, IMongoCollection <TForeignDocument> foreignCollection, BsonDocument let, PipelineDefinition <TForeignDocument, TAsElement> lookupPipeline, Expression <Func <TNewResult, TAs> > @as, AggregateLookupOptions <TForeignDocument, TNewResult> options = null) where TAs : IEnumerable <TAsElement> { Ensure.IsNotNull(aggregate, nameof(aggregate)); return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Lookup <TResult, TForeignDocument, TAsElement, TAs, TNewResult>( foreignCollection, let, lookupPipeline, @as, options))); }
public void Lookup_with_empty_required_params_should_throw_expected_exception() { RequireServer.Check().Supports(Feature.AggregateLet); string warehousesCollectionName = "warehouses"; Assert.Throws <ArgumentNullException>(() => { PipelineStageDefinitionBuilder.Lookup <BsonDocument, BsonDocument, BsonDocument, IEnumerable <BsonDocument>, BsonDocument>( null, null, new EmptyPipelineDefinition <BsonDocument>(), new StringFieldDefinition <BsonDocument, IEnumerable <BsonDocument> >("stockdata") ); }); Assert.Throws <ArgumentNullException>(() => { var client = new MongoClient(CoreTestConfiguration.ConnectionString.ToString()); var warehousesCollection = client.GetDatabase("test").GetCollection <BsonDocument>(warehousesCollectionName); PipelineStageDefinitionBuilder.Lookup <BsonDocument, BsonDocument, BsonDocument, IEnumerable <BsonDocument>, BsonDocument>( warehousesCollection, null, null, new StringFieldDefinition <BsonDocument, IEnumerable <BsonDocument> >("stockdata") ); }); Assert.Throws <ArgumentNullException>(() => { var client = new MongoClient(CoreTestConfiguration.ConnectionString.ToString()); var warehousesCollection = client.GetDatabase("test").GetCollection <BsonDocument>(warehousesCollectionName); PipelineStageDefinitionBuilder.Lookup <BsonDocument, BsonDocument, BsonDocument, IEnumerable <BsonDocument>, BsonDocument>( warehousesCollection, null, new EmptyPipelineDefinition <BsonDocument>(), (StringFieldDefinition <BsonDocument, IEnumerable <BsonDocument> >)null ); }); }
public void Lookup_with_let_should_return_the_expected_result() { RequireServer.Check(); var client = new MongoClient(CoreTestConfiguration.ConnectionString.ToString()); var warehousesCollection = client.GetDatabase("test").GetCollection <BsonDocument>("warehouses"); var lookupPipeline = new EmptyPipelineDefinition <BsonDocument>() .Match(new BsonDocument("$expr", new BsonDocument("$and", new BsonArray { new BsonDocument("$eq", new BsonArray { "$stock_item", "$$order_item" }), new BsonDocument("$gte", new BsonArray { "$instock", "$$order_qty" }) }))) .Project( Builders <BsonDocument> .Projection .Exclude("_id") .Exclude("stock_item")); var result = PipelineStageDefinitionBuilder.Lookup <BsonDocument, BsonDocument, BsonDocument, IEnumerable <BsonDocument>, BsonDocument>( warehousesCollection, new BsonDocument { { "order_item", "$item" }, { "order_qty", "$ordered" } }, lookupPipeline, new StringFieldDefinition <BsonDocument, IEnumerable <BsonDocument> >("stockdata") ); RenderStage(result).Document.Should().Be(@" { '$lookup' : { 'from' : 'warehouses', 'let' : { 'order_item' : '$item', 'order_qty' : '$ordered' }, 'pipeline' : [ { '$match' : { '$expr' : { '$and' : [ { '$eq' : ['$stock_item', '$$order_item'] }, { '$gte' : ['$instock', '$$order_qty'] }] } } }, { '$project' : { '_id' : 0, 'stock_item' : 0 } }], 'as' : 'stockdata' } }"); }