Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

mongo-import.js 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  1. "use strict";
  2. const zlib = require('zlib');
  3. const util = require('util');
  4. const async = require('async');
  5. const io = require('./lib/io');
  6. const MongoClient = require('mongodb').MongoClient;
  7. const miss = require('mississippi');
  8. const options = require('yargs').argv;
  9. const m = {
  10. host: options.host || 'localhost',
  11. port: options.port || '27017',
  12. db: options.db || 'ol'
  13. };
  14. const mongoURI = util.format("mongodb://%s:%s/%s", m.host, m.port, m.db);
  15. const date = options.date || '2016-07-31';
  16. MongoClient.connect(mongoURI, (err, db) => {
  17. if (err) throw err;
  18. console.info('connected to %s', mongoURI);
  19. async.series([
  20. (cb) => importFile(db.collection('authors'), 'authors', cb),
  21. (cb) => importFile(db.collection('works'), 'works', cb),
  22. (cb) => importFile(db.collection('editions'), 'editions', cb)
  23. ], (err) => {
  24. db.close();
  25. if (err) throw err;
  26. });
  27. });
  28. function importFile(collection, name, done) {
  29. var path = util.format('data/ol_dump_%s_%s.txt.gz', name, date);
  30. console.info('loading %s', path);
  31. io.openFile(path, (err, stream) => {
  32. if (err) return done(err);
  33. var dest = miss.to.obj((data, enc, cb) => {
  34. collection.insert(data, {w: 0}, (err, res) => {
  35. cb(err);
  36. });
  37. }, (cb) => {
  38. cb();
  39. done();
  40. });
  41. stream.pipe(zlib.createUnzip())
  42. .pipe(io.decodeLines())
  43. .pipe(dest);
  44. });
  45. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...