0

I try to get all keys and values type in all levels of a mongo collection. I have something like:


    "CRM" : {
            "Teste" : "true"
        },
        "Endereco" : {
            "Cidade" : "MARINGA",
        },
    "Vendas" : [
            {
                "idP" : NumberInt(34),
                "txt" : "001"
            },{
                "idP" : NumberInt(34),
                "txt" : "002"
            }
    ],
    "Tipos" : [
            {
                "idT" : NumberInt(34),
                "idTipo" : NumberInt(34)
            },{
                "idT" : NumberInt(34),
                "idTipo" : NumberInt(34)
            }
    ]
    }

 

I want return like this:

    CRM.Teste : Boolean,
    CRM.Endereco.Cidade : String,
    Vendas[].idP : NumberInt(34),
    Vendas[].txt : String,
    Tipos[].idT : NumberInt(34),
    Tipos[].idTipo : NumberInt(34)

I don't have a model, is there some function in mongo driver or linq that return this?

  • 2
    Does this answer your question? [How get all keys in all level in a mongo collection c#? LinQ Functions](https://stackoverflow.com/questions/73299041/how-get-all-keys-in-all-level-in-a-mongo-collection-c-linq-functions) – dododo Aug 14 '22 at 20:38
  • do not recreate the same question – dododo Aug 14 '22 at 20:38
  • Thanks! But in the other question I only get the keys, now I need the key and the type of value. – Isadora Bastos Aug 14 '22 at 22:43
  • it doesn't matter, solution will be the same – dododo Aug 14 '22 at 22:45
  • If I have a list of string of all my keys, how do I know what is the type of each one? – Isadora Bastos Aug 14 '22 at 22:58
  • analyze BsonType: `BsonValue value = ..; .. = value.BsonType` – dododo Aug 14 '22 at 23:06
  • Thanks! In this code: https://dotnetfiddle.net/qtg4oe that you made, when I analyze BsonValue? I put it in a Dictionary? Or is it possible in a list? – Isadora Bastos Aug 14 '22 at 23:12
  • I got the name and the key in this code: https://dotnetfiddle.net/Port5c I got: ` { "nameKey" : "Vendas", "keyType" : "Array" }, { "nameKey" : "idP", "keyType" : "Int32" } ` But I want this: ` { "nameKey" : "Vendas", "keyType" : "Array" }, { "nameKey" : "Vendas.idP", "keyType" : "Int32" } ` Because I need to know the elements of array. Is there any way, please? – Isadora Bastos Aug 15 '22 at 12:52
  • 1
    Thank you! Now I got: https://dotnetfiddle.net/Port5c – Isadora Bastos Aug 15 '22 at 19:09

0 Answers0