简体   繁体   中英

how to import json file having nested json array without keys into mongodb using nodejs

I'm new to Mongo and want to Import json file with nested json array without key's but i don't know how to parse a this format and then import to mongodb. Let me Explain-->

Here is Json file that i had converted from PDF file :

[  
   [  
      "S. No.",
      "Bank Name",
      "First 4 Letters of ",
      "IFSC",
      "Short Code",
      "Multimodal Code"
   ],
   [  
      "1",
      "Abhyudaya Co-op Bank",
      "ABHY",
      "ACB",
      "*99*87#"
   ],
   [  
      "2",
      "Allahabad Bank",
      "ALLA",
      "ALB",
      "*99*54#"
   ],
   [  
      "3",
      "Andhra Bank",
      "ANDB",
      "ANB",
      "*99*59#"
   ],
   [  
      "4",
      "Apna Sahakari Bank",
      "ASBL",
      "APN",
      "*99*85#"
   ],
   [  
      "5",
      "Axis Bank",
      "UTIB",
      "AXB",
      "*99*45#"
   ]
]

now i want to directly import into my mongodb using node.js expect first array that is irreverent:

[  
      "S. No.",
      "Bank Name",
      "First 4 Letters of ",
      "IFSC",
      "Short Code",
      "Multimodal Code"
 ]

Ok, so after store data in mongodb i want result of find query something like this:

{“banks”: [ 
 {“name”: “Abhyudaya Co-op Bank”,  “ifsc”: “ABHY”, “sc”: “ACB”},
 {“name”: “Allahabad Bank”,  “ifsc”: “ALLA”, “sc”: “ALB”}, 
 {“name”: “Andhra Bank”,  “ifsc”: “ANDB”, “sc”: “ANB”},
 {“name”: “Apna Sahakari Bank”,  “ifsc”: “ASBL”, “sc”: “APN”},
 {“name”: “Axis Bank”,  “ifsc”: “UTIB”, “sc”: “AXB”}
 ]}

Please help me with node.js code or query to import this file in mongo.

try this. I did it for only two parameters you please put the others accordingly. put the code inside a .js file. i guess you have to already create a collection 'tmp' (or what you want) in the database with _id:0 (which I matched here) and "banks":[ ]. I tried to put doc.length in the for loop, somehow it did not work, so I put 5 there.you can try that. check the "tmp" collection after few seconds even if the node command does not end in node.js shell


var MongoClient = require('mongodb').MongoClient;var assert = require('assert');var ObjectId = require('mongodb').ObjectID;var url = 'mongodb://localhost:27017/test';var insertDocument = function(db, callback) {

doc = "put your doc here"
for (i = 1; i <= 5; i++){var a =  doc[i][1];var b =  doc[i][2];db.collection('tmp').update({_id:0},{ "$push" : {"banks" :{"name": a,"ifsc": b}}});}; } 

MongoClient.connect(url, function(err, db) { assert.equal(null, err);insertDocument(db, function() {
  db.close();});});

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM