我需要做什么:
我需要根据日期范围从MongoDB中将大量(未确定大小)的数据导出到CSV。 (最短是一天,没有最大值)
每天的数据量不同。
我的问题:
我考虑过使用#include <iostream>
#include <math.h>
#include <string>
using namespace std;
// Method to make strings of equal length
int makeEqualLength(string& fnum,string& snum){
int l1 = fnum.length();
int l2 = snum.length();
if(l1>l2){
int d = l1-l2;
while(d>0){
snum = '0' + snum;
d--;
}
return l1;
}
else if(l2>l1){
int d = l2-l1;
while(d>0){
fnum = '0' + fnum;
d--;
}
return l2;
}
else
return l1;
}
int singleDigitMultiplication(string& fnum,string& snum){
return ((fnum[0] -'0')*(snum[0] -'0'));
}
string addStrings(string& s1,string& s2){
int length = makeEqualLength(s1,s2);
int carry = 0;
string result;
for(int i=length-1;i>=0;i--){
int fd = s1[i]-'0';
int sd = s2[i]-'0';
int sum = (fd+sd+carry)%10+'0';
carry = (fd+sd+carry)/10;
result = (char)sum + result;
}
result = (char)carry + result;
return result;
}
long int multiplyByKaratsubaMethod(string fnum,string snum){
int length = makeEqualLength(fnum,snum);
if(length==0) return 0;
if(length==1) return singleDigitMultiplication(fnum,snum);
int fh = length/2;
int sh = length - fh;
string Xl = fnum.substr(0,fh);
string Xr = fnum.substr(fh,sh);
string Yl = snum.substr(0,fh);
string Yr = snum.substr(fh,sh);
long int P1 = multiplyByKaratsubaMethod(Xl,Yl);
long int P3 = multiplyByKaratsubaMethod(Xr,Yr);
long int P2 = multiplyByKaratsubaMethod(addStrings(Xl,Xr),addStrings(Yl,Yr)) - P1-P3;
return (P1*pow(10,length) + P2*pow(10,length/2) + P3);
}
int main()
{
string firstNum = "62";
string secondNum = "465";
long int result = multiplyByKaratsubaMethod(firstNum,secondNum);
cout << result << endl;
return 0;
}
,但我需要添加数据库中不存在的列。
我的MongoDB对象并不是单独的,它们仅由文本组成,但我在数据库中有很多它们,需要检索很多它们。因此,当我通过NodeJS使用Mongo来查询我的对象时,我有:
JavaScript堆内存不足
我正在寻找一个长期的解决方案,这样我就不用担心我想要检索多少天的数据。
任何解决方案?
谢谢,
答案 0 :(得分:0)
你可以使用cursor.stream和csv-write-stream来创建这样的内容:
var MongoClient = require('mongodb').MongoClient;
var csvWriter = require('csv-write-stream');
var fs = require('fs');
MongoClient.connect('mongodb://localhost:27017/test', function (err, db) {
var collection = db.collection('big_collection');
var stream = collection.find().stream();
var writer = csvWriter();
writer.pipe(fs.createWriteStream('out.csv'));
stream.on('end', function () {
writer.destroy();
db.close();
});
stream.on('data', function (data) {
Object.assign(data, {'some_extra_column' : ''});
});
stream.pipe(writer);
});