我正在使用:
运行mongoexportmongoexport ... --fields _id, Field1
导致:
{"_id":"someId1","Field1":false}
{"_id":"someId2","Field1":true}
我更愿意获得"标准" CSV输出:
_id, Field1
someId1, false
someId2, true
但我无法找到合适的mongoexport参数。
答案 0 :(得分:0)
使用此:
mongoexport --db db_name --collection collection_name --type=csv --fields _id,Field1 --out /dir/backups/myCollection.csv
如果您使用的是早期版本(mongodb 2.6),那么首先要创建一个名为fields的文件: 的 fields.txt 强>
_id
Field1
然后使用:
mongoexport --db db_name --collection collection_name --csv --fieldFile fields.txt --out /dir/backups/myCollection.csv
答案 1 :(得分:0)
使用此:
package sample.spring.chapter01;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.hibernate.SessionFactory;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionStatus;
//import org.springframework.transaction.TransactionCallback;
//import org.springframework.transaction.TransactionTemplate;
@Repository(value="PieceHibernateDAO")
public class PieceHibernateDAO {
@Autowired
private SessionFactory sessionFactory;
public int createPieceHibernate(final Piece piece) {
sessionFactory.getCurrentSession().save(piece);
return piece.getpieceid();
}
//public Piece getPiece
public void fill(Resource resource) throws FileNotFoundException, IOException{
InputStream is=resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
for (CSVRecord record:parser) {
String ref=record.get("référence ascensoriste").trim();
String asc=record.get("ascensoriste").trim();
//String desc=record.get("Description article").trim();
String prix=record.get("Pv");
String category=record.get("Familie").trim();
//System.out.println(category);
Piece lift_comp=new Piece();
lift_comp.setasc(asc);
//lift_comp.setdesc(desc);
lift_comp.setref(ref);
lift_comp.setprice(prix);
lift_comp.settype(category);
lift_comp.setinfo();
//listp.add(lift_comp);
int cpid=lift_comp.getcompanyid(asc);
lift_comp.setcompanyid(cpid);
//sessionFactory.getCurrentSession().save(lift_comp);
createPieceHibernate(lift_comp);
}
}
}