mongodb mongo脚本中的文件写入操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8971151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File write operations in mongo script?
提问by sudesh
Is this possible to write the query result to the file from mongo js script. I have searched a lot, but I didn't find any solution.
是否可以将查询结果从 mongo js 脚本写入文件。我已经搜索了很多,但我没有找到任何解决方案。
ex:-
前任:-
cursor = db.users.find();
while(cursor.hasNext()) {
cursor.next();
// writing the cursor output to file ????<br/>
}
回答by milan
You could use print and then redirect output:
您可以使用打印然后重定向输出:
script.js:
脚本.js:
cursor = db.users.find();
while(cursor.hasNext()){
printjson(cursor.next());
}
then run the script and redirect output to a file:
然后运行脚本并将输出重定向到一个文件:
mongo --quiet script.js > result.txt
回答by Roman Ivanov
http://www.mongodb.org/display/DOCS/Scripting+the+shellparagraph "Differences between scripted and interactive/ Printing".
http://www.mongodb.org/display/DOCS/Scripting+the+shell段落“脚本化和交互式/打印之间的差异”。
./mongo server.com/mydb --quiet --eval "db.users.find().forEach(printjson);" > 1.txt
回答by Oubenal Mohcine
You can skip the while loop using forEach()
:
您可以使用forEach()
以下命令跳过 while 循环:
db.users.find().forEach(printjson);
回答by fernandohur
Whenever I need to write the result of a mongo query to a local file I generally use the the writeFile(pathToFile, stringContents)
function.
每当我需要将 mongo 查询的结果写入本地文件时,我通常会使用该writeFile(pathToFile, stringContents)
函数。
Example: let's say that you quickly need to find the email of all registered users and send it to your buddy Jim in the marketing department.
示例:假设您需要快速找到所有注册用户的电子邮件并将其发送给营销部门的好友 Jim。
$ mongo mongodb://my-fancy-mongo-server --ssl -u fancy_username -p fancy_password
successfully connected to my-fancy-mongo-server!
> emails = db.users.distinct('email_address')
> writeFile("jims_email_list.json", tojson(emails))
or if Jim expect's a CSV file then
或者如果 Jim 期望是一个 CSV 文件,那么
$ mongo mongodb://my-fancy-mongo-server --ssl -u fancy_username -p fancy_password
successfully connected to my-fancy-mongo-server!
> emails = db.users.distinct('email_address')
> writeFile("jims_email_list.csv", emails.join("\n"))
You can now send Jim the list of emails and save the day!
您现在可以将电子邮件列表发送给 Jim 并节省一天的时间!
To important things to notice about the writeFile
function:
关于writeFile
函数的重要注意事项:
- The second argument must be a string.
- The first argument must be a file that doesn't already exist, otherwise you will get an error.
- 第二个参数必须是字符串。
- 第一个参数必须是一个不存在的文件,否则你会得到一个错误。
回答by Darren Newton
Wouldn't it be simpler to use one of the Mongo driversfor a general purpose language (such as Python, Ruby, Java, etc) and write your results to a file that way, in a format you can use (such as CSV, etc.)?
将Mongo 驱动程序之一用于通用语言(例如 Python、Ruby、Java 等)并将结果以您可以使用的格式(例如 CSV、等等。)?
UPDATE:According to the documentation for mongodumpyou can export a collection with a query:
更新:根据mongodump的文档,您可以使用查询导出集合:
$ ./mongodump --db blog --collection posts
-q '{"created_at" : { "$gte" : {"$date" : 1293868800000},
"$lt" : {"$date" : 1296460800000}
}
}'
However you would need to import that collection back into MongoDB to operate on it or use mongoexportto export as JSON or CSV using the same query flag (-q
) as mongodump
.
然而,你将需要导入采集回的MongoDB在其上或使用操作mongoexportJSON或CSV使用相同的查询标志(出口-q
)的mongodump
。
回答by 1nstinct
For distinct you have to create script.js file with contents:
对于不同的,您必须创建包含内容的 script.js 文件:
mongo = new Mongo("localhost");
doctor = mongo.getDB("doctor");
users = doctor.getCollection("users");
cities = users.distinct("address.city");
printjson(cities);
then in console run:
然后在控制台运行:
mongo --quiet script.js > result.txt