9

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 commands 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

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • 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: http://docs.mongodb.org/ecosystem/drivers/node-js/ – James Wahlin Mar 15 '13 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 Mar 15 '13 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 Mar 18 '13 at 14:27
  • Yea, you need to be doing insert :) – Brad C Mar 29 '13 at 03:45

2 Answers2

4

This is how I do it in my code

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

I exec the mongoimport command 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(command[,options])

Alexandru Olaru
  • 6,842
  • 6
  • 27
  • 53
0

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

Community
  • 1
  • 1
thomers
  • 2,603
  • 4
  • 29
  • 50
  • He said he is aware of that. He's asking how to do the equivalent of `mongoimport` in JavaScript instead of the mongo shell. – Benny Schmidt Jun 08 '15 at 18:42
  • He asked "Is it possible to execute mongoimport in my code". The first article I linked to is titled "Execute A Unix Command With Node.js" - so technically, this would work. But I'm aware that this is probably not what he had in mind in terms of efficiency. – thomers Jun 09 '15 at 18:13