我在反序列化BsonDocuments类型时遇到一个小问题,我找不到原因,但是我有一些线索,也许是关于{0字段},因为错误告诉我我有{0字段}的列。我还意识到它是{}(空数组)。
MY MONGO QUERY ON ROBO 3T AND THE RESULT
我的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());
错误在这里:
var lista = JsonConvert.DeserializeObject<List<UsuariosAcessos>>(result.ToJson());
当我尝试将那个“空数组”反序列化为Model int的时候?我找到了解决方法,因为我只需要知道NU_KIPER_TAG和NU_KIPER_RF中是否包含某些内容,因此,我做了这个新的Mongo查询。 使用$ COND的新蒙古查询
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" ]}}
}
])
我无法将其转换为C#,我努力了,但是我对sintaxe并不熟悉。我还用Google搜索了一个没有成功的示例。
我认为它类似于:
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 ]}, } }
}
}
};
答案 0 :(得分:0)
尝试这种方法,它将起作用!
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" } }}},
}
}
};
空数组将代表:新的BsonDocument {}
{ "NU_TELEFONE", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_TELEFONE", new BsonDocument { } } }}, "","$NU_TELEFONE" } }}},
答案 1 :(得分:0)
如何像这样使用流畅的C#:
public int? NU_KIPER_TAG { get; set; }
public int? NU_KIPER_RF { get; set; }
关于反序列化时的问题,我认为无法将{}(空数组)转换为int或int ?,这似乎是您的反序列化无法正常工作的原因,请尝试更改模型。
从:
public object NU_KIPER_TAG { get; set; }
public object NU_KIPER_RF { get; set; }
收件人:
L.geoJSON(data, {
pointToLayer: function (feature, latlng){
var icon_gisement = L.VectorMarkers.icon({
iconColor: "#FFFFFF"
});
return L.marker(latlng, {
icon: icon_gisement
});
}
}).bindPopup(function (layer) {
return layer.feature.properties.description;
}).addTo(map).openPopup();
这不是您要找的,但是也许您的反序列化将起作用。之后,您可以转换数据。希望这会有所帮助。