简体   繁体   English

MongoDb C#:获取所有匹配的子文档

[英]MongoDb C# : get all matching sub-documents

Suppose I have the below Merchant document structure 假设我具有以下商家文档结构

Merchants 招商

[
   {
      "ID":"1",
      "Name":"Merchant 1",
      "Vendors":[
         {
            "ID":"1",
            "Name":"Vendor 1",
            "Currency":"GBP"
         },
         {
            "ID":"2",
            "Name":"Vendor 2",
            "Currency":"EURO"
         },
         {
            "ID":"3",
            "Name":"Vendor 3",
            "Currency":"GBP"
         }
      ]
   },
   {
      "ID":"2",
      "Name":"Merchant 2",
      "Vendors":[
         {
            "ID":"4",
            "Name":"Vendor 4",
            "Currency":"GBP"
         },
         {
            "ID":"5",
            "Name":"Vendor 5",
            "Currency":"EURO"
         },
         {
            "ID":"6",
            "Name":"Vendor 6",
            "Currency":"GBP"
         }
      ]
   }
]

Now I would like to get merchant whose Id is 2 and all it's vendors whose Currency is GBP. 现在,我想获得ID为2的商人以及货币为GBP的所有商人。

Output would be 输出将是

{
      "ID":"2",
      "Name":"Merchant 2",
      "Vendors":[
         {
            "ID":"4",
            "Name":"Vendor 4",
            "Currency":"GBP"
         },
         {
            "ID":"6",
            "Name":"Vendor 6",
            "Currency":"GBP"
         }
      ]
   }

How could I do this using .Find method in mongodb & C#? 如何在mongodb和C#中使用.Find方法执行此操作?

I am thinking about aggregation framework but I couldn't just make it to work. 我正在考虑聚合框架,但我不能仅仅使其起作用。

db.Merchants.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $match: {
                "ID": "2"
            }
        },

        // Stage 2
        {
            $project: {
                ID: 1,
                Name: 1,
                Vendors: {
                    $filter: {
                        input: "$Vendors",
                        as: "vendor",
                        cond: {
                            $eq: ["$$vendor.Currency", "GBP"]
                        }
                    }
                }
            }
        },

    ]



);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅投影匹配的子文档以及原始文档字段mongodb C# - Projecting only matched sub-documents along with original document fields mongodb C# 如何通过C#类插入带有子文档数组的文档 - How to Insert a document with array of sub-documents by C# class Mongodb C# 驱动程序只返回数组中匹配的子文档 - Mongodb C# driver return only matching sub documents in array 自动生成MongoDB文档内集合中子文档/子文档的值 - Autogenerating values for child/sub-documents in a collection within a MongoDB document MongoDB/C#获取所有文档中某个键的所有值 - MongoDB/C# get all values of a certain key in all documents MongoDB C# 从ID列表中获取所有文档 - MongoDB C# Get all documents from a list of IDs 如何使用 Mongo.Driver.Linq 和 Mongo C# 驱动程序 2.3 返回带有过滤子文档的文档? - How do I return a document with filtered sub-documents using Mongo.Driver.Linq with the Mongo C# driver 2.3? 使用MongoDB C#2.0驱动程序对数组子文档进行DistinctAsync - DistinctAsync against array sub documents with MongoDB C# 2.0 driver C#/ LINQ /获取与条件匹配的子集合的所有元素? - C# / LINQ / get all elements of sub-collection matching a condition? MongoDB C# 驱动程序 - 更新嵌入文档数组中的所有字段 - MongoDB C# Driver - update all fields in embedded array of documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM