Mongo Seeding CLI
Fill your MongoDB database with data in easy way with command line interface (CLI). Use JavaScript and JSON files to define the data!
Looking for different type of database seed solution?
Installation
npm install -g mongo-seeding-cli
Usage
Seed database with data from current directory using default DB connection values:
seed
You can specify your custom settings like this:
seed -u 'mongodb://127.0.0.1:27017/mydb' -d ./samples/data
or, like this:
seed --db-host 127.0.0.1 --db-name testing2 -d ./samples/data/ --drop-database --replace-id
Command line parameters
Here are all command line parameters you can use:
Name | Required | Default Value | Description |
---|
--data $PATH or -d $PATH | no | (directory, where seed command is executed) | Path to directory containing import data |
--db-uri $URI or -u $URI | no | undefined | If defined, the URI will be used for estabilishing connection to database, ignoring values defined via other db-* parameters, i.e. db-name , db-host , etc. |
--db-protocol $DB_PROTOCOL | no | mongodb | MongoDB database protocol |
--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 |
--db-username $DB_USERNAME | no | database | Username for connecting with database that requires authentication |
--db-password $DB_PASSWORD | no | database | Password for connecting with database that requires authentication |
--drop-database | no | false | If parameter specified, drops database before importing data |
--replace-id | no | false | If parameter specified, replaces id property with _id for every object it imports |
--reconnect-timeout | no | 10 (seconds) | Maximum time of waiting for successful MongoDB connection |
--help or -h | n/a | n/a | Shows help |
Preparing data to import
-
Create a new base directory. In this example, it'll be named data
.
-
Define a few collections via creating subdirectories in data
directory. New collection will be created if it doesn't exist in database.
Naming convention
- If you don't care about import order - just name directories simply with collection names - i.e.
categories
, posts
, comments
, etc. - To keep your own import order, construct directory name with a number, separator and actual collection name - i.e.
1-categories
, 2_posts
, 3.comments
, 4 tags
, etc. Supported separators between import number and collection name: -
, _
, .
or space.
-
We have collections - now it's time to define documents. It can be done via creating files in collections directories.
A few things to know:
- Collection directory can contain multiple files
- Every file can contain single objects or array of objects
- One object represents one MongoDB document
- Supported extensions:
js
, json
- In
js
files export object or array of objects via module.exports = objectOrArray
.
Some examples:
object.js
( will result in creating single MongoDB document):
module.exports = {
name: "Parrot"
}
array.js
(it will create 2 documents):
module.exports = [
{
name: "Dog"
},
{
name: "Cat"
}
]
object.json
(represent 1 MongoDB document):
{
"name": "Penguin",
}
array.json
(creates two different documents):
[
{
"name": "Hamster"
},
{
"name": "Crocodile"
}
]
-
The complete file structure should look like this:
data
+-- 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
-
To sum everything up: Subdirectories of base directory represent database collections. Files in collection directories represent documents. Simple as that.
Samples
Take a look at samples repository to see more examples how to define data structure properly!