How to get nested values from JSON

Hi. I am new to mongo and we are in our company starting new developments with this engine.
I have the following json

{
  "_id": {
    "$oid": "xxxxxxxxxx"
  },
  "logs": [
    {
      "request": {
        "alternate_intents": true,
        "context": {
          "conversation_id": "xxx",
          "system": {
            "dialog_turn_counter": 1,
            "dialog_request_counter": 1,
            "session_id": "xxx",
            "assistant_id": "xxxx",
            "skill_reference_s": "test Skill"
          },
          "metadata": {}
        },
        "input": {
          "options": {
            "return_context_s": "true"
          },
          "text": "por cuál por el primero ",
          "message_type_s": "text"
        }
      },
      "response": {
        "intents": [
          {
            "intent": "Consulta_Motivo_Renuncia",
            "confidence": 0.36768287420272827
          },
          {
            "intent": "Solicita_Rut",
            "confidence": 0.324138343334198
          },
          {
            "intent": "Ejemplos",
            "confidence": 0.2719978451728821
          },
          {
            "intent": "Solicita_Numero_de_Serie",
            "confidence": 0.26375070810317996
          },
          {
            "intent": "Solicita_Direccion_Cliente",
            "confidence": 0.25889307856559757
          },
          {
            "intent": "Solicita_Email_Cliente",
            "confidence": 0.25696601867675783
          },
          {
            "intent": "Protocolo_Renuncia_Parte3",
            "confidence": 0.2559263825416565
          },
          {
            "intent": "Explica_Protocolo",
            "confidence": 0.2487402230501175
          },
          {
            "intent": "Respuesta_Cliente_Positiva",
            "confidence": 0.24280670285224915
          },
          {
            "intent": "Solicita_Telefono_Contacto",
            "confidence": 0.2413155049085617
          }
        ],

and need to list all intents for each “conversation_id” for example

"intent": "Respuesta_Cliente_Positiva",
            "confidence": 0.4656783819198609

I would really appreciate if you can give me an example of a query to get that information

Regards

Rodrigo

Since

I suggest https://university.mongodb.com/.

1 Like

Welcome to the community @Rodrigo_Ceballos :grinning: !

As @steevej said, it would be better for you to start with the MongoDB University free courses, follow the developer learning path. It’s the best for you.

If you really need help with your query, can you provide us a complete sample document ?
The one you shared is incomplete, at the end we don’t necessarily know what the “logs” array looks like, are there a lot of elements in it ? More than once object with the same “conversation_id” field ?

Finally to be honest, it’s a big document, I’m not convinced of the data schema. The last course of the developer learning path is about data modeling, I strongly encourage you to follow it.

2 Likes

@Rodrigo_Ceballos

Your document structure has arrays, nested documents (a.k.a. objects or embedded documents or sub-documents) and nested arrays.

In general, to query a collection you use the MongoDB Query Language (MQL)'s db.collection.find method.

For more complex querying and results, you use the Aggregation-Framework. The result you are looking for can be built using an aggregation query with the db.collection.aggregate() method.

Hi, I’m on it (MongoDB University free courses). Is that we needed to make a report with this json in a way more urgently.

here is the document in case you can give me an idea. If the document is optimal or not.

Thank you. Regards

Rodrigo

I would like to know if with that document, I have a way to bring me all “intent”: “Deseo_Renuncia” and confidence

Regards Rodrigo.

Hi @Rodrigo_Ceballos

With your current data schema, it’s complicated. You can’t just display “intention”: “Deseo_Renuncia” and confidence with a simple find(). You would have to use the Aggregation Framework, and even with that it could be tedious.

12 437 lines in a single document, that was bound to be a problem one day :laughing:.
You made the mistake of thinking that with MongoDB you can store all relationships in a single document, too much denormalization. Technically it’s possible but in practice it leads to many many problems for queries or for your indexing strategy.

I definitely recommend that you follow M320.

1 Like