gpt4 book ai didi

mongodb - 蒙哥 : export all fields data from collection without specifying fields?

转载 作者:IT老高 更新时间:2023-10-28 13:24:51 27 4
gpt4 key购买 nike

  • 我有超过 100 个字段,我正在寻找一种方法,以便我可以将整个集合导出为 CSV 格式

  • 命令行要求通过

  • 提供所有字段

-f [ --fields ] arg comma seperated list of field names e.g. -f name,age

  • 有没有办法像使用 dump 但不是 bson 格式来获取整个集合?
  • 我需要 CSV 数据

谢谢

最佳答案

在 bash 中,您可以创建此“export-all-collections-to-csv.sh”并将数据库名称作为唯一参数传递(随意将其简化为单个集合):

OIFS=$IFS;
IFS=",";

dbname=$1 #put "database name" here if you don't want to pass it as an argument

collections=`mongo $dbname --eval "rs.slaveOk();db.getCollectionNames();" --quiet`;
collectionArray=($collections);

for ((i=0; i<${#collectionArray[@]}; ++i));
do
keys=`mongo $dbname --eval "rs.slaveOk();var keys = []; for(var key in db.${collectionArray[$i]}.findOne()) { keys.push(key); }; keys;" --quiet`;
mongoexport --db $dbname --collection ${collectionArray[$i]} --fields "$keys" --csv --out $dbname.${collectionArray[$i]}.csv;
done

IFS=$OIFS;

关于mongodb - 蒙哥 : export all fields data from collection without specifying fields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8949335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com