简体   繁体   English

$ Project中的$ Cond和$ Eq-C#驱动程序MongoDB

[英]$Cond and $Eq Inside $Project - C# Driver MongoDB

Im facing a litle problem in Deserializing BsonDocuments type, I couldn't find the reason, but I have some clues, maybe, it's about {0 fields}, because the error tel me about the column I have {0 fields}. 我在反序列化BsonDocuments类型时遇到了一个小问题,我找不到原因,但是我有一些线索,也许是关于{0个字段},因为错误告诉我我有{0个字段}的列。 I also realised that its a {} (empty array). 我还意识到它是{}(空数组)。

MY MONGO QUERY ON ROBO 3T AND THE RESULT 我在ROBO 3T上的MONGO查询和结果

MY C# MONGO QUERY 我的C#MONGO查询

 var connString = "mongodb+srv";
            var client = new MongoClient(connString);
            var database = client.GetDatabase("Base");
            var collection = database.GetCollection<BsonDocument>("collection");

            var match1 = new BsonDocument("$match", new BsonDocument("PartnerId", "2021"));
            var match2 = new BsonDocument("$match", new BsonDocument("CD_CLIENTE", codCond));

            var project = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { "CD_CLIENTE", 1 }, { "CD_ACESSO", 1 },
                 { "ID_ACESSO", 1 },{ "NOME", 1 },{ "NU_TELEFONE", 1 }, { "EMAIL", 1 }, { "NU_KIPER_RF", 1 },  { "NU_KIPER_TAG", 1 },
                  { "FG_KIPER_MOBILE", 1 },{ "KEY_HASH", 1 },}}}; MY MONGO AND RESULT

            var sort = new BsonDocument("$sort", new BsonDocument("NOME", 1));

            var pipeline = new[] { match1, match2, project, sort };
            var result = collection.Aggregate<BsonDocument>(pipeline).ToList();

            var lista = JsonConvert.DeserializeObject<List<UsuariosAcessos>>(result.ToJson());

The error is here: 错误在这里:

        var lista = JsonConvert.DeserializeObject<List<UsuariosAcessos>>(result.ToJson());

Exatly when I try to Deserialise an that "empty array" to Model int?. 当我尝试将那个“空数组”反序列化为Model int吗? I found a work around, because I just need to know if, we have or dont not something in NU_KIPER_TAG and NU_KIPER_RF, so, I did this new Mongo query. 我找到了解决方法,因为我只需要知道NU_KIPER_TAG和NU_KIPER_RF中是否包含某些内容,因此,我做了这个新的Mongo查询。 NEW MONGO QUERY USING $COND 使用$ COND的新MONGO查询

db.dbACESSO.aggregate([

{
    $match: { PartnerId: "2021", CD_CLIENTE: 4003}
},

{
    $project: {_id:0, CD_CLIENTE:1, CD_ACESSO:1, ID_ACESSO:1, NOME:1, NU_TELEFONE:1,EMAIL:1, FG_KIPER_MOBILE:1,
       TAG:{$cond: [{ $eq: [ "$NU_KIPER_TAG", {}]}, 0, 1 ]}, CONTROLE:{$cond: [{ $eq: [ "$NU_KIPER_RF", {}]}, 0, 1 ]},            
       APPATIVO:{$cond: [{ $eq: [ "$KEY_HASH", {}]}, "", "$KEY_HASH" ]}}

}

])

I couldnt translate it to C#, I tried hard, but i'm not familiarized with the sintaxe. 我无法将其翻译为C#,我努力了,但是我对sintaxe并不熟悉。 I also googled for a sample with no success. 我还用谷歌搜索了一个没有成功的样本。

I think its something like: 我认为它类似于:

var project = new BsonDocument {
                {
                    "$project", new BsonDocument { { "_id", 0 }, { "CD_CLIENTE", 1 }, { "CD_ACESSO", 1 },{ "ID_ACESSO", 1 },{ "NOME", 1 }
                        ,{ "NU_TELEFONE", 1 }, { "EMAIL", 1 }, { "NU_KIPER_RF", 1 },  { "NU_KIPER_TAG", 1 },{ "FG_KIPER_MOBILE", 1 },{ "KEY_HASH", 1 },


                        {"TAG", new BsonDocument{{"$cond", new BsonDocument {{ "$eq",  "$NU_KIPER_TAG", "{}"}}, 0, 1 ]}, } }
                    }
                }
            }; 

Try this way, it will work! 尝试这种方式,它将起作用!

var project = new BsonDocument
            {
                {
                    "$project", new BsonDocument
                    {
                        { "_id", 0},
                        { "CD_CLIENTE", 1},
                        { "CD_ACESSO", 1 },
                        { "NOME", 1},
                        { "EMAIL", 1 },
                        { "FG_KIPER_MOBILE", 1 },
                        { "GRUPO", "$GRUPO.NM_DESCRICAO" },
                        { "UNIDADE", "$UNIDADE.NM_DESCRICAO" },                        
                        { "NU_TELEFONE", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_TELEFONE", new BsonDocument { } } }}, "","$NU_TELEFONE" } }}},
                        { "TAG", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_KIPER_TAG", new BsonDocument { } } }}, 0,1 } }}},
                        { "CONTROLE", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_KIPER_RF", new BsonDocument { } } }}, 0,1 } }}},
                        { "APPATIVO", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$KEY_HASH", new BsonDocument { } } }}, "", "$KEY_HASH" } }}},
                    }
                }
            };

The empty array will be represented for: new BsonDocument { } 空数组将表示为: new BsonDocument {}

{ "NU_TELEFONE", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_TELEFONE", new BsonDocument { } } }}, "","$NU_TELEFONE" } }}},

What about using the fluent C# like this: 像这样使用流畅的C#怎么办:

    var connString = "mongodb+srv";
    var client = new MongoClient(connString);
    var database = client.GetDatabase("Base");
    var collection = database.GetCollection<UsuariosAcessos>("collection"); //Here you put you Model

    var filter = Builders<UsuariosAcessos>.Filter.Eq(x => x.PartnerId, cliente) 
        & Builders<UsuariosAcessos>.Filter.Eq(x => x.CD_CLIENTE, codCond);   

    var lista = collection.Aggregate().Match(filter).Project(x => new UsuariosAcessos
    {
          CD_CLIENTE = x.CD_CLIENTE,
          ID_ACESSO = x.ID_ACESSO,
          CD_ACESSO = x.CD_ACESSO,
          NOME = x.NOME,
          NU_TELEFONE = x.NU_TELEFONE,
          EMAIL = x.EMAIL,
          NU_KIPER_RF = x.NU_KIPER_RF,
          NU_KIPER_TAG = x.NU_KIPER_TAG,
          FG_KIPER_MOBILE = x.FG_KIPER_MOBILE,
          KEY_HASH = x.KEY_HASH
  }).ToList();

About your problem at Deserialization time, I think it's not possible to translate {} (empty array) to int or int?, it's appear to be the reason your deserialization is not working, try to change your model. 关于反序列化时的问题,我认为无法将{}(空数组)转换为int或int ?,这似乎是反序列化无法正常工作的原因,请尝试更改模型。

FROM: 从:

public int? NU_KIPER_TAG { get; set; }        
public int? NU_KIPER_RF { get; set; } 

TO: 至:

public object NU_KIPER_TAG { get; set; }        
public object NU_KIPER_RF { get; set; }

Its not you are looking for, but, maybe your Deserialization are going to Work. 不是您要找的,而是您的反序列化将起作用。 After that, you can convert the data. 之后,您可以转换数据。 Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM