Mongo Seeding CLI
Fill your MongoDB database with data in easy way with CLI. Use JavaScript and JSON files to define the data!
If this is not what are you looking for...
Installation
npm install -g mongo-seeding-cli
Usage
seed --db-host 0.0.0.0 --db-name testing2 -d ./samples/data/ --drop-database --convert-id
Command Line Parameters
Name | Required | Default Value | Description |
---|
--data $PATH or -d $PATH | yes | - | Path to directory containing import data structure specified above |
--db-host $DB_HOST | no | 127.0.0.1 | MongoDB database host |
--db-port $DB_PORT | no | 27017 | MongoDB database port |
--db-name $DB_NAME | no | database | Name of the database |
--drop-database | no | false | If parameter specified, drop database before importing data |
--convert-id | no | false | If parameter specified, replaces id field with _id for every object it imports |
Prepare your data to import
-
Create a new directory. In this example, name it simply as data
.
-
This directory data
will contain subdirectories in following naming convention: $NUMBER-$COLLECTION_NAME
- i.e. 1-categories
, 2-posts
.
$COLLECTION_NAME
- it's just a collection name that will be created if not exist.
$NUMBER
- to define collections import order. If you don't care about import order - just name the directory as $COLLECTION_NAME
, i.e. categories
.
-
In every collection directory you can create multiple files in formats: .js
and .json
. You can define object or array of objects.
For JS files: Export object or array of objects you want to import via module.exports = objectOrArray
Every object is a representation of document in MongoDB database.
-
The whole file structure should look like this:
data
+-- tsconfig.json // Optional for using TypeScript (see Advanced )
+-- 1-categories
| +-- cat.js
| +-- dogs.js
| +-- other-animals.json
+-- 2-posts
| +-- post-about-my-cat.json
| +-- dog-posts.js
| +-- random-stuff.js
+-- 3-media
| +-- cat-image.js
| +-- dog.js
Samples
Take a look at samples repository to see Mongo Seeding in action!