Skip to content

Instantly share code, notes, and snippets.

@libook
Last active April 28, 2017 09:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libook/2fc41c21eceba77173cab633c1c4f307 to your computer and use it in GitHub Desktop.
Save libook/2fc41c21eceba77173cab633c1c4f307 to your computer and use it in GitHub Desktop.
Read file by line and batch insert documents into MongoDB.
'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
// }
});
});
{
"name": "read-line-and-insert",
"version": "0.0.1",
"description": "",
"main": "0_0read-line-and-insert.js",
"author": {
"name": "libook",
"email": "libook7@gmail.com",
"url": "https://github.com/libook"
},
"license": "WTFPL",
"dependencies": {
"line-reader": "^0.4.0",
"mongodb": "^2.2.26"
}
}
@Chunlin-Li
Copy link

[找不到点赞就自己生产一个]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment