admin管理员组

文章数量:1313121

Is there any library available in node.js/javascript that allows an individual to use mongoimport in code?

To my understanding, mongoimport is kinda like an .exe, which you have to execute it first before being able to use its text input environment.

Is it possible to execute mongoimport in my code and then parse whatever mands I need directly in my code?

My current algorithm involves:

fs.appendFile('log.txt',JSON.stringify(obj, null, 2));

obj is an object which specifies what functions to parse into JSON.stringify with the res method of node.js (which requests HTTP responses)

var obj = {};
obj.url = hostNames[i];
obj.statusCode = res.statusCode;
obj.headers = res.headers;

Then I use mongoimport to import this JSON doc into my MongoDB.

mongoimport --host localhost -db scrapeapp -collection scrape --file log.txt --jsonArray

This method is obviously inefficient. I would like to do all these steps in one going.

Help appreciated

Is there any library available in node.js/javascript that allows an individual to use mongoimport in code?

To my understanding, mongoimport is kinda like an .exe, which you have to execute it first before being able to use its text input environment.

Is it possible to execute mongoimport in my code and then parse whatever mands I need directly in my code?

My current algorithm involves:

fs.appendFile('log.txt',JSON.stringify(obj, null, 2));

obj is an object which specifies what functions to parse into JSON.stringify with the res method of node.js (which requests HTTP responses)

var obj = {};
obj.url = hostNames[i];
obj.statusCode = res.statusCode;
obj.headers = res.headers;

Then I use mongoimport to import this JSON doc into my MongoDB.

mongoimport --host localhost -db scrapeapp -collection scrape --file log.txt --jsonArray

This method is obviously inefficient. I would like to do all these steps in one going.

Help appreciated

Share Improve this question asked Mar 15, 2013 at 16:00 theGreenCabbagetheGreenCabbage 4,84519 gold badges86 silver badges175 bronze badges 4
  • Have you looked at the MongoDB node.js driver? This is what you want to use if you would like to talk directly from a node.js service to MongoDB. Documentation is here: docs.mongodb/ecosystem/drivers/node-js – James Wahlin Commented Mar 15, 2013 at 16:24
  • To extend what James said, I think you have something of a mis-understanding over what mongoimport is used for. Most of your interactions with mongodb will occur through a driver. The driver allows you to insert, update, and delete documents (objects) in your database through a specific language, in this case javascript. Mongoimport should not be used for normal insertions in your db, it is mainly used for importing data that has been exported from other sources, like other databases or applications. – ACE Commented Mar 15, 2013 at 17:50
  • You are right. I do currently have a misunderstanding with the use of MongoDB. I am currently scraping headers from a couple (thousand) sites, which I will then do analysis of in a couple month's time. I will change my algorithm to do an insert() header by header instead of doing an import. What do you think? db.scrape.insert(JSON.stringify(obj,null,2)); – theGreenCabbage Commented Mar 18, 2013 at 14:27
  • Yea, you need to be doing insert :) – Brad C Commented Mar 29, 2013 at 3:45
Add a ment  | 

2 Answers 2

Reset to default 4

This is how I do it in my code

let exec = require('child_process').exec
let mand = 'mongoimport -d database -c collection --file import.json'
exec(mand, (err, stdout, stderr) => {
  // check for errors or if it was succesfuly
  cb()
})

I exec the mongoimport mand and then I do pass the cb next the code to be accesible, or if you do not use an asynchronous style you can do it synchronously with child_process.execSync(mand[,options])

I'm in no way a node expert - but if you have existing JSON files, you could execute mongoimport in Node as shell mand as described here or in various answers.

本文标签: jsonExecuting mongoimport inside code with JavascriptNodejsStack Overflow