Home > Blockchain >  MongoDb Aggregation match two fields C# (Fluent aggregation)
MongoDb Aggregation match two fields C# (Fluent aggregation)

Time:09-15

I use C# MongoDb Driver and I try to build Aggregation with Fluent aggregation pipelines.

In some stage in my aggregation I use MATCH that has to find documents if some field in document equals to another.

For example, I have this documents:

[
  { 
   "Product" : "PROD1", 
    "SubField" : "2", 
    "ShortDescription" : "string", 
    "LongDescription" : "string", 
    "SubFields" : {
        "Field" : "1" 
    }
  },
  { 
   "Product" : "PROD2", 
    "SubField" : "3", 
    "ShortDescription" : "string", 
    "LongDescription" : "string", 
    "SubFields" : {
        "Field" : "3" 
   }
  }
]

In Studio 3T this MATCH stage code works OK:

{
   $expr: {$eq: ["$SubField", "$SubFields.Field"]}
}

I tried in C# this code, but it does not work:

.Match( x => x["SubField"] == x["SubFields.Field"])

I would appreciate help,

TIA, Yaakov.

CodePudding user response:

.net driver allows passing raw mql instead typed. So try this:

.Match("{ $expr: {$eq: ["$SubField", "$SubFields.Field"] } }")
  • Related