2

I am trying to save a document in mongoDB database. However I am not able to achieve this.

Below is my Code-

db.system.js.save({ _id:"createMultipleUsers", value:
    function() { 
        try {
            var query="";
            for(var i=0;i<2;i++ ) { 
                if(i !=0) {
                    query=query+",";  
                }
                query=query+"{ _id: "+i+", mobileNumber: "+i+", emailID: \"email"+i+"@gmail.com\"}";
            } 
            query="[ "+query+" ]";
           //print(query);
           db.User.insertMany(query);
       } catch (e) {
           print (e);
       }
   } 
});

Any help/hint is appreciated.

Thank you in advance.

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
Pulla
  • 21
  • 3

2 Answers2

0

Well, as I can see in your db.system.js.save, you are passing a string value to insertMany how ever its recommended to pass JSON document instead-

Example-

try {
   db.products.insertMany( [
      { item: "card", qty: 15 },
      { item: "envelope", qty: 20 },
      { item: "stamps" , qty: 30 }
   ] );
} catch (e) {
   print (e);
}

Source- https://docs.mongodb.com/v3.2/reference/method/db.collection.insertMany/#examples

Similar Question- How to insert multiple documents at once in MongoDB through Java

In your Case-

Insert query after converting into JSON Dictionary/Document-

//parse string query into json format.
db.User.insertMany(JSON.parse(query));

Hoping this will help you :)

Feel free to shoot your queries.

Community
  • 1
  • 1
Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
  • db.system.js.save({ _id:"createMultipleUsers", value: function() { try { var query=""; for(var i=0;i<2;i++ ) { if(i !=0) { query=query+","; } query=query+"{ _id: "+i+", mobileNumber: "+i+", emailID: \"email"+i+"@gmail.com\"}"; } query="[ "+query+" ]"; print(query); db.User.insertMany(JSON.parse(query)); } catch (e) { print (e); } } }); – Pulla Apr 07 '17 at 14:42
  • ------------------------ But i am getting output as below [ { _id: 0, mobileNumber: 0, emailID: "email0@gmail.com"},{ _id: 1, mobileNumber: 1, emailID: "email1@gmail.com"} ] SyntaxError: JSON.parse: expected property name or '}' at line 1 column 5 of the JSON data – Pulla Apr 07 '17 at 14:42
  • if json structure is not proper then we get this error.. I am glad you corrected it... – Vikash Pandey Apr 07 '17 at 18:37
0
db.system.js.save({ _id:"createMultipleUsers", value:
    function() { 
        try {
            var query="";
            for(var i=0;i<2;i++ ) { 
                if(i !=0) {
                    query=query+",";  
                }
                query=query+"{ \"_id\": "+i+", \"mobileNumber\": "+i+", \"emailID\": \"email"+i+"@gmail.com\"}";
            } 
            query="[ "+query+" ]";
           print(query);
           db.User.insertMany(JSON.parse(query));
       } catch (e) {
           print (e);
       }
   } 
});
Rahul Hendawe
  • 902
  • 1
  • 14
  • 39
Pulla
  • 21
  • 3
  • Hi Vikash Pandey Thanks for your help – Pulla Apr 07 '17 at 16:16
  • Output : ------------ [ { "_id": 0, "mobileNumber": 0, "emailID": "email0@gmail.com"},{ "_id": 1, "mobileNumber": 1, "emailID": "email1@gmail.com"} ] Its inserting data to db also The errors for the question asked is resolved by 2 lines changes as below – Pulla Apr 07 '17 at 16:17
  • The errors for the question asked is resolved by 2 lines changes as below – Pulla Apr 07 '17 at 16:17
  • 1. query=query+"{ _id: "+i+", mobileNumber: "+i+", emailID: \"email"+i+"@gmail.com\"}"; TO query=query+"{ \"_id\": "+i+", \"mobileNumber\": "+i+", \"emailID\": \"email"+i+"@gmail.com\"}"; – Pulla Apr 07 '17 at 16:17
  • 2. db.User.insertMany(query); TO db.User.insertMany(JSON.parse(query)); – Pulla Apr 07 '17 at 16:17
  • glad to help :). if my ans was useful for you then accept it :) – Vikash Pandey Apr 07 '17 at 18:36
  • yeah it means you had to insert a proper json instead of string.. correct? – Vikash Pandey Apr 07 '17 at 18:36