0

I am writing a script in Nodejs to map a json to mongodb but I am really confused on how to tackle this as the structure is really complex. Any help would be appreciated! So basically the structure looks like this.

{
    "4.1": {
        "picture": "",
        "code": {
            "PEK": {
                "number": 4.1,
                "text": "XYZ."
            },
            "DXB": {
                "number": 5.1,
                "text": "XYZ"
            },
            "LAX": {
                "number": 6.1,
                "text": "XYZ"
            },
            "HND": {
                "number": 6.1,
                "text": "XYZ"
            }
        }
    },
    "4.2": {
        "picture": "",
        "province": {
            "PEK": {
                "number": 4.2,
                "text": "XYZ"
            },
            "DXB": {
                "number": 5.2,
                "text": "XYZ"
            },
            "LAX": {
                "number": 6.2,
                "text": "XYZ"
            },
            "HND": {
                "number": 6.2,
                "text": "XYZ"
            }
        }
    }
}

So I have put two instances for example just so it's easier to understand. What I want is that I am using the the number of "PEK" as a key to access the right object. When I get response from the server I get a number, and the code, for example 4.1 and DXB. Using this information I want to show DXB under 4.1 and also its respective image. All the objects under 4.1 are tied to a common image which is linked to 4.1 but they have their own respective number and text which I want to show at runtime depending on the response. What would be the smartest way to map this to mongodb. Any help would be appreciated, thanks!!

BleachedAxe
  • 393
  • 2
  • 5
  • 20
  • 1
    This is a very bad structure for use with MongoDB. See [MongoDB Query Help - query on values of any key in a sub-object](https://stackoverflow.com/q/19802502/2313887). Basically if this structure came from some other response it should be reformatted as either single documents or singular "grouped" documents with arrays and consistent properties. Which of those to choose is really up to how your application needs to use the data. – Neil Lunn Mar 30 '19 at 13:32
  • This response is not coming from the server. This JSON is set up as is to be compatible with another API that is using it for a different purpose. What I want to do is write a script in nodejs that would map this to mongodb in an efficient way while keeping the relationships that i mentioned. I know it's a bad structure to have stored in mongodb that's why I am asking on how can I write a script to map it in an efficient way. I am struggling to think of an algorithm, I can write the code once I have that in mind. – BleachedAxe Mar 30 '19 at 13:41
  • 1
    And what I said is "which is best?" is basically too broad a question. It's not up to someone else to tell you what structure to use, but rather up to **you** to structure data how **your application uses it**. Hence, pointing you at a reference answer showing a particular form that does apply. Point is you *"should"* have data like `{ "code": "PEK", "number": 4.1, "text": "XYZ" }`. But whether that is as documents or arrays is not up to anyone here to tell you. You need to decide the structure yourself. – Neil Lunn Mar 30 '19 at 13:46
  • 1
    And furthermore, if you want to know "How do I manipulate JavaScript objects from this form into this target form?" then that's really a bit of a different question, and also reliant on that **decision** as mentioned earlier. – Neil Lunn Mar 30 '19 at 13:47

0 Answers0