Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://flaviocopes.com/node-mongodb/
  2.  
  3. const mongo = require('mongodb').MongoClient;
  4. const url = 'mongodb://localhost:27017';
  5.  
  6. mongo.connect(url, {
  7.     useNewUrlParser: true,
  8.     useUnifiedTopology: true
  9.   }, (err, client) => {
  10.   if (err) {
  11.     console.error(err)
  12.     return
  13.   }
  14.  
  15.   if (client) {
  16.       random_read(client);
  17.   }
  18. });
  19.  
  20. function random_read(client) {
  21.     const db = client.db('write_test');
  22.     const collection = db.collection('test_queue');
  23.    
  24.     let s = new Date();
  25.     collection.find({scraped: true}).toArray((err, items) => {
  26.         let e = new Date();
  27.        
  28.         if (err) console.error(err);
  29.        
  30.         if (items) {
  31.             console.log(`Found ${items.length} items in ${e-s}ms`);
  32.         }
  33.        
  34.         client.close();
  35.     });
  36. }
  37.  
  38. /**
  39.  * Inserting 500000 items takes 5149ms
  40.  * Inserting 100000 items takes 1073ms
  41.  */
  42. function bulk_insert(client) {
  43.     const db = client.db('write_test');
  44.  
  45.     const collection = db.collection('test_queue');
  46.  
  47.     let urls = [];
  48.     let n = 200000;
  49.  
  50.     for (let i = 0; i < n; i++) {
  51.         urls.push({
  52.             url: `https://someurltoscrape-${i}.com`,
  53.             scraped: Math.random() > .9,
  54.         });
  55.     }
  56.  
  57.     let s = new Date();
  58.  
  59.     collection.insertMany(urls, (err, result) => {
  60.         if (err) console.error(err);
  61.        
  62.         //console.log(result);
  63.         let e = new Date();
  64.         console.log(`Inserting ${n} items takes ${e-s}ms`);
  65.         client.close();
  66.     });
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement