npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-stream-helper

v1.0.0

Published

CLI tool for MongoDB data tasks

Downloads

62

Readme

Mongo Stream Helper

Mongo Stream Helper is a CLI tool designed to handle background tasks related to MongoDB data, such as downloading large collections using streaming and transferring data between collections. This tool can be used to manage MongoDB data more efficiently and can be packaged as an npm package for wider use.

Features

  • Download large MongoDB data: Stream and save data to the local disk.
  • Transfer MongoDB data: Move data from one collection to another.
  • CLI and Config File Support: Accept parameters through CLI inputs or a configuration file.
  • Progress Tracking: Monitor the download process with progress indicators.

Installation

First, clone the repository and navigate to the project directory:

git clone https://github.com/vasudeogaichor/mongo-stream-helper.git
cd mongo-stream-helper

Install the dependencies:

npm install

Installation

CLI Usage

You can run the CLI tool using npx and ts-node:

npx ts-node src/index.ts <command> [options]

Commands

  • download: Download data from a MongoDB collection to a local file.

Options

  • mongodbUri (string, required): MongoDB URI for connecting to the database.
  • databaseName(string, required): Name of the database.
  • sourceCollection (string, required): Name of the source collection.
  • filterQuery(string, optional): Filter query in JSON format to apply to the collection.
  • skip(number, optional): Number of documents to skip.
  • limit(number, optional): Limit the number of documents to download.
  • downloadLocation(string, required): Directory to save the downloaded file.
  • filename(string, required): Name of the downloaded file.
  • config(string, optional): Path to a JSON configuration file containing the above options.

Example

  1. Download data using CLI options:
npx ts-node src/index.ts download --mongodbUri "mongodb://root:example@localhost:27017" --databaseName "testdb" --sourceCollection "testcollection" --filterQuery "{}" --skip 0 --limit 1000 --downloadLocation "home/username/Downloads" --filename "data.json"
  1. Download data using a configuration file: Create a configuration file downloadConfig.json:
{
  "mongodbUri": "mongodb://root:example@localhost:27017",
  "databaseName": "testdb",
  "sourceCollection": "testcollection",
  "filterQuery": {},
  "skip": 0,
  "limit": 1000,
  "downloadLocation": "./downloads",
  "filename": "data.json"
}

Run the download command with the configuration file:

npx ts-node src/index.ts download --config downloadConfig.json


3. Run below command and answer the subsequent prompts:
```bash
npx ts-node src/index.ts download

- **transfer**: Transfer data from one MongoDB collection to another collection.

### Options
- **`sourceMongodbUri`** (string, required): MongoDB URI for connecting to the database source.
- **`sourceDatabaseName`**(string, required): Name of the source database.
- **`sourceCollection`** (string, required): Name of the source collection.
- **`filterQuery`**(string, optional): Filter query in JSON format to apply to the source collection.
- **`skip`**(number, optional): Number of documents to skip.
- **`limit`**(number, optional): Limit the number of documents to download.
- **`targetMongodbUri`** (string, required): MongoDB URI for connecting to the database target.
- **`targetDatabaseName`**(string, required): Name of the target database.
- **`targetCollection`** (string, required): Name of the target collection.
- **`updateExisting`** (boolean, optional): Include if you want to update existing documents else skip.
- **`config`**(string, optional): Path to a JSON configuration file containing the above options.

### Example
1. Transfer data using CLI options:
```bash
npx ts-node src/index.ts transfer --sourceMongodbUri "mongodb://root:example@localhost:27017" --sourceDatabaseName "testdb" --sourceCollection "testcollection1" --filterQuery "{}" --skip 0 --limit 1000 --targetMongodbUri "mongodb://root:example@localhost:27017" --targetDatabaseName "testdb" --targetCollection "testcollection2" --updateExisting
  1. Transfer data using a configuration file: Create a configuration file transferConfig.json:
{
  "sourceMongodbUri": "mongodb://root:example@localhost:27017",
  "sourceDatabaseName": "testdb",
  "sourceCollection": "testcollection1",
  "filterQuery": {},
  "skip": 0,
  "limit": 10000,
  "targetMongodbUri": "mongodb://root:example@localhost:27017",
  "targetDatabaseName": "testdb",
  "targetCollection": "testcollection2",
  "updateExisting": true
}

Run the download command with the configuration file:

npx ts-node src/index.ts transfer --config transferConfig.json
  1. Run below command and answer the subsequent prompts:
npx ts-node src/index.ts transfer

Development

Building the Project

To build the project, run:

npm run build

Running the Tests

Prerequisites

Ensure you have the required test dependencies installed:

npm install --save-dev mocha chai ts-mocha

Generating Test Data

Before running the tests, generate test data using the generateData script:

  1. Use src/generateData.ts script to upload data to a test collection with faker.js:
  2. Run the script:
    ts-node src/generateData.ts
  3. Update the credentials in the test scripts.
  4. To run the tests, use the following command:
    npm run test:dev