Socket
Socket
Sign inDemoInstall

mongo-seeding-cli

Package Overview
Dependencies
4
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongo-seeding-cli

Fill your MongoDB database with data in easy way with CLI. Use JavaScript and JSON files to define the data!


Version published
Maintainers
1
Install size
3.79 MB
Created

Readme

Source

Mongo Seeding

Mongo Seeding CLI

npm version David David Codacy Badge

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:

NameRequiredDefault ValueDescription
--data $PATH or -d $PATHno(directory, where seed command is executed)Path to directory containing import data
--db-uri $URI or -u $URInoundefinedIf 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_PROTOCOLnomongodbMongoDB database protocol
--db-host $DB_HOSTno127.0.0.1MongoDB database host
--db-port $DB_PORTno27017MongoDB database port
--db-name $DB_NAMEnodatabaseName of the database
--db-username $DB_USERNAMEnodatabaseUsername for connecting with database that requires authentication
--db-password $DB_PASSWORDnodatabasePassword for connecting with database that requires authentication
--drop-databasenofalseIf parameter specified, drops database before importing data
--replace-idnofalseIf parameter specified, replaces id property with _id for every object it imports
--reconnect-timeoutno10 (seconds)Maximum time of waiting for successful MongoDB connection
--help or -hn/an/aShows help

Preparing data to import

  1. Create a new base directory. In this example, it'll be named data.

  2. 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.
  3. 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"
      }
    ]
    
  4. 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
    
  5. 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!

Keywords

FAQs

Last updated on 07 Mar 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc