简体   繁体   中英

Can I read a csv file inside of a Mongo Shell Javascript file?

Basically I have a file of strings and I need to query against mongo to see if they exist in our database.

So I need to loop through a file and query mongo with a find query using the string from the file and then look at the results and increment some counters I have.

I am trying to do this using a shell script and calling mongo using --eval option but it's running slow. It's been over an hour and hasn't finished a 120,000 queries. I am thinking it would be faster if I could do it in a javascript file so it doesn't have to make the connection for each query.

Thanks for any suggestions!

Karen

Better option would be to use the native shell method cat() .

Lets say, your file has following content and situated at /home/desktop/myfile.txt

row1
row2
row3
row4

Following code will let you get the file content as array in string var:

$> var string = cat('home/desktop/myfile.txt');
$> string = string.split('\n');
$> db.records.find({field: {$in: string}});

For bulk imports like this, you're usually better off with mongoimport . You may have to munge the CSV file to meet the requirements, but it will perform the final database operation over a single connection.

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