我可以在Mongo Shell Javascript文件中读取csv文件吗? [英] Can I read a csv file inside of a Mongo Shell Javascript file?
问题描述
基本上我有一个字符串文件,我需要查询mongo以查看它们是否存在于我们的数据库中.
Basically I have a file of strings and I need to query against mongo to see if they exist in our database.
因此,我需要遍历文件并使用文件中的字符串使用查找查询来查询mongo,然后查看结果并增加一些计数器.
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.
我正在尝试使用shell脚本并使用--eval选项调用mongo,但运行速度很慢.已经过了一个多小时,还没有完成120,000个查询.我认为如果可以在javascript文件中执行此操作会更快,这样就不必为每个查询建立连接.
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.
谢谢您的建议!
凯伦
推荐答案
Better option would be to use the native shell method cat().
可以说,您的文件具有以下内容,位于/home/desktop/myfile.txt
Lets say, your file has following content and situated at /home/desktop/myfile.txt
row1
row2
row3
row4
以下代码将使您以 string var:
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}});
这篇关于我可以在Mongo Shell Javascript文件中读取csv文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!