Search code examples
c#mongodbmongodb.driver

Filter by schema mongodb drive c#


Does anyone know if I can run this schema on the mongo unit of c #. I also generate a BsonArray, but I don't know how to compile it in context, could anyone help? The original schema below is working, I just changed the names of the fields so there might be a syntax error, but I don't know how to run it in c#.

C#:

_db.GetCollection.Aggregate()....

BsonArray:

return new BsonArray
            {
                new BsonDocument("$limit", 1),
                new BsonDocument("$project", new BsonDocument("_id", "$$REMOVE")),
                new BsonDocument("$lookup", new BsonDocument
                {
                    { "from", "table1" },
...... 
Schema:
[
   {
      "$limit":1
   },
   {
      "$project":{
         "_id":"$$REMOVE"
      }
   },
   {
      "$lookup":{
         "from":"Table1",
         "pipeline":[
            {
               "$match":{
                  "date":{
                     "$gte":"ISODate(""2010-04-29T00:00:00.000Z"")",
                     "$lt":"ISODate(""2030-04-29T00:00:00.000Z"")"
                  }
               }
            },
            {
               "$addFields":{
                  "id":"$_id",
                  "value":1
               }
            },
            {
               "$project":{
                  "_id":0,
                  "typevalue":0
               }
            }
         ],
         "as":"Table1"
      }
   },
   {
      "$lookup":{
         "from":"Table2",
         "pipeline":[
            {
               "$match":{
                  "dateInit":{
                     "$gte":"ISODate(""2010-04-29T00:00:00.000Z"")",
                     "$lt":"ISODate(""2030-04-29T00:00:00.000Z"")"
                  },
                  "externalId":1,
               }
            },
            {
               "$addFields":{
                  "id":"$_id",
                  "number":"$number",
                  "client":"$client",
                  "date":"$dateInit",
                  "value":"$value",
                  "status":0,
                  "event":[],
                  "file":"$arrayByte"
               }
            },
            {
               "$project":{
                  "_id":0,
                  "date":0,
                  "arrayByte":0,
                  "Rows":0
               }
            }
         ],
         "as":"Table2"
      }
   },
   {
      "$project":{
         "union":{
            "$concatArrays":[
               "$Table1",
               "$Table2"
            ]
         }
      }
   },
   {
      "$unwind":"$union"
   },
   {
      "$replaceRoot":{
         "newRoot":"$union"
      }
   },
   {
      "$sort":{
         "date":-1
      }
   },
   {
      "$skip":1
   }
]

Solution

  • public static BsonDocument[] GetList()
           {
               return new BsonDocument[]
               {
                   new BsonDocument("$limit", 1),
                   new BsonDocument("$project", new BsonDocument("_id", "$$REMOVE")),
                   new BsonDocument("$lookup", new BsonDocument
                   {
                       { "from", "Table1" },
                       { "pipeline", new BsonArray}
    .......
    
    var list = await _db<Collection>.GetCollection.Aggregate<BsonDocument>(GetList()).ToListAsync();
    
    if (list.Any())
    var myList= list.Select(x => BsonSerializer.Deserialize<MyObject>(x)).ToList();