Last active
April 28, 2017 09:14
-
-
Save libook/2fc41c21eceba77173cab633c1c4f307 to your computer and use it in GitHub Desktop.
Read file by line and batch insert documents into MongoDB.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const mongoUrl = 'mongodb://my-db-address:27017';// MongoDB connection URL. | |
const filePath = 'id.list';// This must be Unix/Linux ID list file. | |
const limit = 100;// Buffer this number of documents. Then do insert. | |
const dbName = 'myDB';// DB name. | |
const collectionName = 'myCollection';// Collection name. | |
const MongoDB = require('mongodb'); | |
const MongoClient = MongoDB.MongoClient, assert = require('assert'); | |
const ObjectId = MongoDB.ObjectID; | |
const lineReader = require('line-reader'); | |
/** | |
* Template for creating documents. | |
*/ | |
function Template(userId) { | |
const date = new Date(); | |
return { | |
"dialog": true, | |
"method": "open", | |
"listType": "notification", | |
"userId": userId, | |
"batchId": ObjectId("111111111111111111111111"),// Please generate a new ObjectId. | |
"read": false, | |
"content": { | |
"title": "This is the title", | |
"body": "This is the body!", | |
"image": "http://image.png", | |
"url": "https://jump-to-a-page.html", | |
"buttonLabel": "I'm a button." | |
}, | |
"receivedDate": date, | |
"endDate": date, | |
"readDate": null | |
}; | |
} | |
/** | |
* Connect to DB server. | |
*/ | |
MongoClient.connect(mongoUrl, function (err, db) { | |
assert.equal(null, err); | |
console.log("Connected successfully to server"); | |
/** | |
* Select collection. | |
*/ | |
const notifications = db.db(dbName).collection(collectionName); | |
/** | |
* Buffer of documents. | |
* Buffer to limit. Then do insert. | |
*/ | |
let documents = []; | |
/** | |
* Read file by line. | |
*/ | |
lineReader.eachLine(filePath, function (line, last, callback) { | |
console.log(line); | |
/** | |
* Push new document to the buffer zone. | |
*/ | |
documents.push(new Template(new ObjectId(line))); | |
if ((documents.length >= limit) || (last)) { | |
/** | |
* The buffer zone is up to limit. | |
* Or this is the last line. | |
*/ | |
/** | |
* Do insert. | |
*/ | |
notifications.insert(documents, function (err) { | |
/** | |
* Process error. | |
*/ | |
if (err) { | |
console.error(err.stack); | |
process.exit(1); | |
} else { | |
/** | |
* Clean the buffer zone. | |
*/ | |
documents = []; | |
if (last) { | |
/** | |
* This is the last line. | |
* Exit. | |
*/ | |
console.log('Done!'); | |
db.close(() => { | |
process.exit(0); | |
}); | |
} else { | |
/** | |
* Continue read lines. | |
*/ | |
callback(); | |
} | |
} | |
}); | |
} else { | |
/** | |
* Continue read lines. | |
*/ | |
callback(); | |
} | |
// if (/* done */) { | |
// return false; // stop reading | |
// } | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "read-line-and-insert", | |
"version": "0.0.1", | |
"description": "", | |
"main": "0_0read-line-and-insert.js", | |
"author": { | |
"name": "libook", | |
"email": "[email protected]", | |
"url": "https://github.com/libook" | |
}, | |
"license": "WTFPL", | |
"dependencies": { | |
"line-reader": "^0.4.0", | |
"mongodb": "^2.2.26" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[找不到点赞就自己生产一个]