Home How can we form a mongoDB schema object in JavaScript for the JSON where keys are changing dynamically?
Reply: 1

How can we form a mongoDB schema object in JavaScript for the JSON where keys are changing dynamically?

CodeHunter
1#
CodeHunter Published in 2018-02-13 15:10:55Z

I want to create a schema in mongoDB for using in an AngularJS project. But the keys for the json are changing dynamically. The json is like this:

{

    “1” : {

         “Name” : “John”,

         “City” : “London”

     },

    “2” : {

         “Name” : “Paul”,

         "City” : “New York”

      }

}

Here the keys are changing. They will always be in integer format for sure. For the values, I can have my schema like this: (using Javascript)

var dbObject = new Schema({
    Name: String,
    City: String
});

I am not sure how to include the key which will always be an Integer in this schema. Any pointers for creation of such a schema object in mongoDB?

Rahul Sharma
2#
Rahul Sharma Reply to 2018-02-13 15:31:17Z

try this

var data = {
    "1": {
        "Name": "John",
        "City": "London"
    },
    "2": {
        "Name": "John",
        "City": "London"
    }
};


for (let key in data) {
    var val = data[key]; // get inner objects
    // call api
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.388743 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO