数据处理

csv处理

  • i-csv
  • import-csv
  • export-csv
  • json2csv
  • fastcsv

json2csv

json to csv converter

https://github.com/i5ting/json2csv

import-csv

https://github.com/i5ting/import-csv

默认使用gbk编码

  1. var import_csv = require('import-csv')
  2. import_csv('test.csv',function(err, data){
  3. console.log(data);
  4. })

指定字符编码机

  1. var import_csv = require('import-csv')
  2. import_csv('test.csv',function(err, data){
  3. console.log(data);
  4. }, 'gbk')

export-csv

https://github.com/i5ting/export-csv

默认使用gbk编码

  1. var export_csv = require('.')
  2. var data = [
  3. {a:1,b:2},
  4. {a:2,b:2},
  5. {a:3,b:2}
  6. ]
  7. export_csv(data, 'test1.csv')

过滤item,重写数据

  1. var export_csv = require('.')
  2. var data = [
  3. { a: 1, b: 2 },
  4. { a: 2, b: 2 },
  5. { a: 3, b: 2 }
  6. ]
  7. export_csv(data, 'test3.csv', function (item) {
  8. for (var key in item) {
  9. item[key] = 'yy +' + item[key];
  10. }
  11. return item;
  12. })

指定回调函数

  1. var export_csv = require('.')
  2. var data = [
  3. {a:1,b:2},
  4. {a:2,b:2},
  5. {a:3,b:2}
  6. ]
  7. export_csv(data, 'test2.csv', function (item) {
  8. return item;
  9. }, function () {
  10. console.log('end...');
  11. })

指定回调函数,并生成header

  1. var export_csv = require('.')
  2. var data = [
  3. {a:1,b:2},
  4. {a:2,b:2},
  5. {a:3,b:2}
  6. ]
  7. export_csv(data, 'test2.csv', function (item) {
  8. return item;
  9. }, function () {
  10. console.log('end...');
  11. }, true)

mongodb如何统计

  1. var json2csv = require('json2csv');
  2. require('../db.js');
  3. var fs = require('fs');
  4. var Order = require('../app/models/order');
  5. var Activity = require('../app/models/activity');
  6. var Wechat = require('../app/models/wechat');
  7. var Contact = require('../app/models/contact');
  8. function main(){
  9. Order.model.aggregate([
  10. {$match : {status2 : {$exists : true} }},
  11. {$group : {_id : "$activity" , count : {$sum : "$product_count"}}}
  12. ]).exec(function (err, data) {
  13. var obj = [];
  14. for (var i = data.length - 1; i >= 0; i--) {
  15. var json = {
  16. "url" : "http://shop.mengxiaoban.cn/iscroll.html?id=" + String(data[i]._id),
  17. "count" : String(data[i].count)
  18. }
  19. obj.push(json);
  20. console.log(json.url);
  21. }
  22. var opts = {
  23. data: obj,
  24. fields: ['url','count'],
  25. fieldNames: ['url','count'],
  26. quotes: ''
  27. };
  28. json2csv(opts, function(err, csv) {
  29. if (err) console.log(err);
  30. fs.writeFile('./1119todayurl.csv', csv, function(err) {
  31. if (err) throw err;
  32. console.log('file saved');
  33. });
  34. });
  35. })
  36. }
  37. }
  38. main();

AGGREGATION 关联

《SQL to Aggregation Mapping Chart》

http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/

优化查询速度

了解索引优化

  1. ContactSchema = new Schema({
  2. ...
  3. owner: {
  4. type: Schema.ObjectId,
  5. required: true,
  6. index: true
  7. }
  8. });

也可以这样的

  1. ContactSchema.ensureIndexes(owner);

了解explain

  1. db.usermodels.find({
  2. '_id' :{
  3. "$gt" :ObjectId("55940ae59c39572851075bfd")
  4. }
  5. }).explain()

关注点

  • stage:查询策略
  • nReturned:返回的文档行数
  • needTime:耗时(毫秒)
  • indexBounds:所用的索引

http://docs.mongodb.org/v3.0/reference/explain-results/

了解profile

profile级别有三种:

  • 0:不开启
  • 1:记录慢命令,默认为大于100ms
  • 2:记录所有命令
  • 3、查询profiling记录
    开启
  1. db.setProfilingLevel(2, 20)

默认记录在system.profile中

  1. db['system.profile'].find()

MONGODB的部署

  • replset
  • shard

我写的《 mongodb运维之副本集实践》

https://cnodejs.org/topic/5590adbbebf9c92d17e734de