本文介绍了如何使用管道和工具进行$ lookup C#中的let参数(MongoDB.Driver 2.7.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C#代码中的MongoDB中运行以下查询.我使用MongoDB.Driver 2.7.2和MongoDB 4.0.

I need to run the following query in MongoDB in my C# code. I use MongoDB.Driver 2.7.2 and MongoDB 4.0.

我想使用$ lookup而不是$ project/$ unwind来防止命名集合的每个可能字段.

I would like to use $lookup rather than $project / $unwind in order to prevent naming each one of the collection's possible fields.

db.getCollection('Collection1').aggregate([  
    { "$match" : { "Id" : 1 } },
        { "$lookup": {
            "from": "Collection2",
            "let": { collection1Id : "$Id" },
            "pipeline": [
                { $match:
                    { $expr:
                        { $and:
                            [
                                { $eq : [ "$Collection1Id",  "$$collection2Id" ] },
                                { $in : [ "$_id" , [1, 2, 3]] }
                            ]
                            }
                        }
                    }
            ],
            "as": "item"
        }
    } 
])

我希望在多个联接条件下将Collection1与Collection2联接的输出.

I expect the output of Collection1 joined with Collection2 under multiple join conditions.

推荐答案

由于您没有指定特定的类映射和示例文档,因此下面的答案将使用手册BsonDocument类型和示例数据https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#specify-multiple-join-conditions-with-lookup"rel =" nofollow noreferrer> $ lookup:使用查找<

As you didn't specify a particular class mapping and example documents, below answer will use BsonDocument type and example data from the manual $lookup: specify multiple join conditions with lookup

BsonArray subpipeline = new BsonArray();

subpipeline.Add(
    new 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"} )
             }  
        )
    ))
);

var lookup = new BsonDocument("$lookup", 
                 new BsonDocument("from", "warehouses")
                             .Add("let", 
                                 new BsonDocument("order_item", "$item")
                                             .Add("order_qty", "$ordered"))
                             .Add("pipeline", subpipeline)
                             .Add("as", "stockdata")
);

var results = collection.Aggregate()
                        .Match(new BsonDocument("_id", 1))
                        .AppendStage<BsonDocument>(lookup).ToEnumerable();

foreach (var x in results)
{
    Console.WriteLine(x.ToJson());
} 

请注意,支持更具表现力的 $ lookup 使用 PipelineDefinitionBuilder 版本2.8.x.有关更多信息,请参见 CSHARP-2013

Please note that support for more expressive $lookup using PipelineDefinitionBuilder is coming for version 2.8.x. For more information see CSHARP-2013

这篇关于如何使用管道和工具进行$ lookup C#中的let参数(MongoDB.Driver 2.7.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:27