How to insert multiple fields using loop

80 views
Skip to first unread message

Angelia Agustina Telaumbanua

unread,
Jul 8, 2015, 12:34:43 AM7/8/15
to mongod...@googlegroups.com
Hello, I'm working on mongodb right now and have to insert dummy data about 1000. I am confuse of where must I insert the "for" syntax in mongodb. Here is the docs I want to insert:
Application
{
"name": "Dyned Live",
  "users": [
{
"id": "1001"
"devices": [
{
"type": "a",
"id": "fasdfas123123" 
},
{
"type": "b",
"id": "fasdfas123123"
}
]
},
{
"id": "1002"
"devices":
[{
"type": "a",
"id": "fasdfas123123" 
},
{
"type": "b",
"id": "fasdfas123123"
}
]
}
As you see, I have to loop in the "id"
I wonder if there is one of you can help me to solve this one. Thanks in advance :)


Rhys Campbell

unread,
Jul 8, 2015, 5:35:05 AM7/8/15
to mongod...@googlegroups.com
Hi Angelina,

What language are you using? What format is your source data held in?

If you're sharding I think perhaps an objectId would be a better choice of id. I think maybe enforcing uniqueness using this document structure will be difficult (maybe someone else could chip in there.)

Here a sample Javascript solution that may help you on your way...

var docs = [
{
    "name": "Dyned Live",
    "users": [
        {
            "id": "",
            "devices": [
                {
                    "type": "a",
                    "id": "fasdfas123123"
                },
                {
                    "type": "b",
                    "id": "fasdfas123123"
                }
            ]
        },
        {
            "id": "",
            "devices": [
                {
                    "type": "a",
                    "id": "fasdfas123123"
                },
                {
                    "type": "b",
                    "id": "fasdfas123123"
                }
            ]
        }
    ]
},
{
    "name": "Rhys",
    "users": [
        {
            "id": "",
            "devices": [
                {
                    "type": "a",
                    "id": "rhys"
                },
                {
                    "type": "b",
                    "id": "rhys"
                }
            ]
        },
        {
            "id": "",
            "devices": [
                {
                    "type": "a",
                    "id": "rhys"
                },
                {
                    "type": "b",
                    "id": "rhys"
                }
            ]
        }
    ]
}
];

var inc = 1;

for (var i = 0; i < docs.length; i++) {
var current_doc = docs[i];
        print(current_doc.users.length)
for(var x = 0; x < current_doc.users.length; x++)
{
current_doc.users[x].id = inc;
                inc++;
}
        print(current_doc)
}



This outputs the following...

/* 1 */
{
    "name" : "Dyned Live",
    "users" : [ 
        {
            "id" : 1.0000000000000000,
            "devices" : [ 
                {
                    "type" : "a",
                    "id" : "fasdfas123123"
                }, 
                {
                    "type" : "b",
                    "id" : "fasdfas123123"
                }
            ]
        }, 
        {
            "id" : 2.0000000000000000,
            "devices" : [ 
                {
                    "type" : "a",
                    "id" : "fasdfas123123"
                }, 
                {
                    "type" : "b",
                    "id" : "fasdfas123123"
                }
            ]
        }
    ]
}

/* 2 */
{
    "name" : "Rhys",
    "users" : [ 
        {
            "id" : 3.0000000000000000,
            "devices" : [ 
                {
                    "type" : "a",
                    "id" : "rhys"
                }, 
                {
                    "type" : "b",
                    "id" : "rhys"
                }
            ]
        }, 
        {
            "id" : 4.0000000000000000,
            "devices" : [ 
                {
                    "type" : "a",
                    "id" : "rhys"
                }, 
                {
                    "type" : "b",
                    "id" : "rhys"
                }
            ]
        }
    ]
}


Hope this helps.

Rhys


 
Reply all
Reply to author
Forward
0 new messages