Skip to content

Instantly share code, notes, and snippets.

@decadef20
Created March 21, 2018 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save decadef20/09fab684ea4b2b7c140f7f7a039234a0 to your computer and use it in GitHub Desktop.
Save decadef20/09fab684ea4b2b7c140f7f7a039234a0 to your computer and use it in GitHub Desktop.
nodejs connect mongodb
const request = require('request');
const fs = require('fs');
const MongoClient = require('mongodb').MongoClient;
const dbUrl = "mongodb://localhost:27017/dbname";
const url = '';
const ids = 1000000;
const base = 1;
const downloading = [];
const genOptions = (id)=> ({
method: 'GET',
url: `${url}/users/${id}`,
headers: {
authorization: ''
});
async function delay(msec) {
return new Promise(resolve => setTimeout(() => resolve(), msec));
}
const fetchUsers = async (id) => {
request(genOptions(id), function (error, response, body) {
if (error) {
console.log('error ', error);
return;
}
console.log('downloading length = ', downloading.length,' time = ', Date.now())
if(body) {
downloading.push(JSON.parse(body).data[0])
if(Number.isInteger(downloading.length / 1000)){
MongoClient.connect(dbUrl, function(err, db) {
if(err) {
return console.trace('trace ', err);
}
const collection = db.collection('users');
collection.insertMany(downloading.splice(0,1000) , function(err, result) {
if(err) {
console.log(err);
} else {
console.log('insert ok')
db.close();
}
});
});
}
}
});
}
const users = Array.from({length:ids},(a,b,c) => b+base )
async function run() {
for(let id of users) {
await fetchUsers(id)
await delay(10)
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment