├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.lock 3 | *.log 4 | node_modules 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | services: 3 | - mongodb 4 | sudo: false 5 | node_js: 6 | - "4" 7 | - "5" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2015 Sofish Lin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![build status](https://travis-ci.org/sofish/mongoimport.svg?branch=master) 2 | 3 | # mongoimport 4 | 5 | import JSON to mongodb, associate with [sofish/log2json](https://github.com/sofish/log2json) to manage nginx logs. 6 | 7 | ```php 8 | $ npm install mongoimport --save 9 | ``` 10 | 11 | ## Usage 12 | 13 | Install the package with npm, and bring it to your project. 14 | 15 | ```js 16 | var mi = require('mongoimport'); 17 | mi(config); 18 | ``` 19 | 20 | Follow the codes below to create a config object: 21 | 22 | ```js 23 | var config = { 24 | fields: [], // {array} data to import 25 | db: 'name', // {string} name of db 26 | collection: 'collection' // {string|function} name of collection, or use a function to 27 | // return a name, accept one param - [fields] the fields to import 28 | 29 | // they're options 30 | host: 'localhost:27017', // {string} [optional] by default is 27017 31 | username: 'sofish', // {string} [optional] 32 | password: '***' // {string} [optional] 33 | callback: (err, db) => {} // {function} [optional] 34 | }; 35 | ``` 36 | 37 | ## Test 38 | 39 | Simply run `npm test` to see what happens. 40 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const mongodb = require('mongodb'); 4 | const client = mongodb.MongoClient; 5 | 6 | module.exports = bot; 7 | 8 | function bot(config) { 9 | /* Bot that helps to import your data into db 10 | * @param {object} config 11 | * { 12 | * fields: [], // {array} data to import 13 | * db: 'name', // {string} name of db 14 | * collection: 'collection' // {string|function} name of collection, or return a name 15 | * host: 'localhost:27017', // {string} [optional] by default is 27017 16 | * username: 'sofish', // {string} [optional] 17 | * password: '***' // {string} [optional] 18 | * callback: (err, db) => {} // {function} [optional] 19 | * } 20 | */ 21 | 22 | if(!config.host) config.host = '127.0.0.1:27027'; 23 | if(!config.callback) config.callback = () => {}; 24 | 25 | var callback = config.callback; 26 | var auth = config.username ? `${config.username}:${config.password}@` : ''; 27 | client.connect(`mongodb://${auth}${config.host}/${config.db}`, (err, db) => { 28 | if(err) return callback(err); 29 | 30 | if(!config.fields || !config.fields.length) { 31 | callback(null); 32 | return db.close(); 33 | } 34 | 35 | // remove empty fields; 36 | let fields = config.fields.filter(item => !!item); 37 | if(!fields.length) return db.close(); // fields can be empty 38 | 39 | var c = config.collection; 40 | var collections = {}; 41 | 42 | // map collection 43 | if(typeof c === 'function') { 44 | fields.forEach(item => { 45 | var name = c(item); 46 | if(collections[name]) return collections[name].push(item); 47 | collections[name] = [item]; 48 | }) 49 | } else if(typeof c === 'string') { 50 | collections[c] = fields; 51 | } else { 52 | callback({messsage: 'not matched, no `collection` is specific'}); 53 | } 54 | 55 | var i = 0, l = Object.keys(collections).length - 1; 56 | for(let c in collections) { 57 | db.collection(c).insertMany(collections[c], (err, ret) => { 58 | if(i++ === l) db.close(); 59 | if(err) return callback(err); 60 | callback(null, ret); 61 | }); 62 | } 63 | }); 64 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mongoimport", 3 | "version": "1.0.6", 4 | "description": "import JSON to mongodb collection", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node ./test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/sofish/mongoimport.git" 12 | }, 13 | "keywords": [ 14 | "mongodb", 15 | "json", 16 | "import" 17 | ], 18 | "author": "sofish (http://sofi.sh/)", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/sofish/mongoimport/issues" 22 | }, 23 | "homepage": "https://github.com/sofish/mongoimport", 24 | "dependencies": { 25 | "mongodb": "^2.0.48" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const mongoimport = require('../'); 4 | var host = '127.0.0.1:27017'; 5 | var db = 'mongoimport'; 6 | var collection = function(field) { 7 | return field.name; 8 | }; 9 | var fields = [ 10 | { name: 'sofish', createdAt: '1986', isBot: 'guess me', isFun: 'try me' }, 11 | { name: 'perf', foo: 'bar' }, 12 | { name: 'error', hello: 'world' } 13 | ]; 14 | var config = {host, db, collection, callback, fields} 15 | 16 | mongoimport(config, callback); 17 | 18 | function callback(err, ret) { 19 | if(err) { 20 | if(err.message.match('ECONNREFUSED')) console.log('✘ make sure you have started mongodb server'); 21 | if(err.message.match('Authentication')) console.log('✘ make sure the username/password pair is matched'); 22 | console.log('= done!\n'); 23 | throw err.message; 24 | } 25 | 26 | console.log('✔ %d records inserted', ret.insertedCount); 27 | console.log('= done!\n'); 28 | } --------------------------------------------------------------------------------