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

stream-to-mongo-db-fabienjuif

v1.4.0

Published

Stream data directly to MongoDB

Downloads

5

Readme

Alt text Build Status Code Climate

Support this package

Please support this package by starring it on Github

Stream To Mongo DB

stream-to-mongo-db allows you to stream objects directly into a MongoDB databases, using a read stream (an a S3 file, local file, a Web API or even another MongoDB database). The best thing about this package is it allows you to control the size of the batch before issuing a write to mongo - see CONFIG

SUPPORTED NODE VERSIONS

This package supports Node.js versions 8+. If you require another version to be supported, please raise an issue.

USAGE

npm i stream-to-mongo-db

EXAMPLES

Example 1: Stream from another MongoDB database

Example 1.1: Using MongoDB Client

const MongoClient     = require('mongodb').MongoClient;
const streamToMongoDB = require('stream-to-mongo-db').streamToMongoDB;

// where the data will come from
const inputDBConfig  = { dbURL : 'mongodb://localhost:27017/yourInputDBHere', collection : 'yourCollectionHere'  };
// where the data will end up
const outputDBConfig = { dbURL : 'mongodb://localhost:27017/streamToMongoDB', collection : 'devTestOutput' };

MongoClient.connect(inputDBConfig.dbURL, (error, db) => {
    if(error) { throw error; }

    // create the writable stream
    const writableStream = streamToMongoDB(outputDBConfig);

    // create readable stream and consume it
    const stream = db.collection(inputDBConfig.collection).find().stream();

    stream.pipe(writableStream);

    stream.on('end', () => {
        console.log('done!');
        db.close();
    });
});

Example 1.2: Using Mongoose

const streamToMongoDB = require('stream-to-mongo-db').streamToMongoDB;
const mongoose        = require('mongoose');

// where the data will come from
const connection = mongoose.connect('mongodb://localhost:27017/streamToMongoDB');
const MyModel    = mongoose.model('ModelName', mySchema);

// where the data will end up
const outputDBConfig = { dbURL : 'mongodb://localhost:27017/streamToMongoDB', collection : 'devTestOutput' };

// create the writable stream
const writableStream = streamToMongoDB(outputDBConfig);

// create readable stream and consume it
const stream = MyModel.find().lean().stream();

stream.pipe(writableStream);

stream.on('end', () => {
    console.log('done!');
    connection.close();
});

This example gets even more powerful when you want to transform the input data before writing it to the writableStream:

[...]

// create the readable stream and transform the data before writing it
const stream = MyModel.find().lean().stream({
    transform: (doc) => {
        // do whatever you like to the doc
        doc.whoIsAwesome = 'StreamToMongoDBIsAwesome';
    }
});

stream.pipe(writableStream);

stream.on('end', () => {
    console.log('done!');
    connection.close();
});

Example 2: Stream from an S3 file using AWS-SDK

const streamToMongoDB = require('stream-to-mongo-db').streamToMongoDB;
const AWS             = require('aws-sdk');
const JSONStream      = require('JSONStream');

const s3              = new AWS.S3();
const params          = { Bucket: 'myBucket', Key: 'myJsonData.json' };

// where the data will end up
const outputDBConfig = { dbURL : 'mongodb://localhost:27017/streamToMongoDB', collection : 'devTestOutput' };

// create the writable stream
const writableStream = streamToMongoDB(outputDBConfig);

// create readable stream and consume it
s3.getObject(params).createReadStream()
    .pipe(JSONStream.parse('*'))
    .pipe(writableStream);

Example 3: Stream from a Web API

const streamToMongoDB = require('stream-to-mongo-db').streamToMongoDB;
const request         = require('request');
const JSONStream      = require('JSONStream');

// where the data will end up
const outputDBConfig = { dbURL : 'mongodb://localhost:27017/streamToMongoDB', collection : 'devTestOutput' };

// create the writable stream
const writableStream = streamToMongoDB(outputDBConfig);

// create readable stream and consume it
request('www.pathToYourApi.com/endPoint')
    .pipe(JSONStream.parse('*'))
    .pipe(writableStream);

Example 4: Stream from a local file

const streamToMongoDB = require('stream-to-mongo-db').streamToMongoDB;
const JSONStream      = require('JSONStream');
const fs              = require('fs');

// where the data will end up
const outputDBConfig = { dbURL: 'mongodb://localhost:27017/streamToMongoDB', collection: 'devTestOutput' };

// create the writable stream
const writableStream = streamToMongoDB(outputDBConfig);

// create readable stream and consume it
fs.createReadStream('./myJsonData.json')
    .pipe(JSONStream.parse('*'))
    .pipe(writableStream);

CONFIG

  • dbURL

    [ REQUIRED - String ]

    The url to your db (including the db name)

    eg: mongodb://localhost:27017/streamToMongoDB

  • collection

    [ REQUIRED - String ]

    The collection to stream to

    eg: myCollection

  • batchSize

    [ OPTIONAL [ default : 1 ] - Integer ]

    The number of documents consumed from the read stream before writing to mongodb

    This option defaults to 1, i.e: write every object individually to mongoDB as it is received. This default is ideal if want to ensure every object is written as soon as possible without the possibility of losing any objects if the MongoDB connection is interrupted.

    However, in most cases, this is unnecessary, since writing every object individually will incur an additional I/O cost. You can change this option to, say 100, which will batch these writes in 100's; allowing you to consume the stream must faster.

    eg: 100

  • insertOptions

    [ OPTIONAL [ default : { w : 1 } ] - Object ]

    MongoDB insert options

    This option defaults to { w : 1 }, i.e: requests acknowledgement that the write operation has propagated to the standalone mongod or the primary in a replica set

    eg: see mongo documentation for other options

CONTRIBUTION

Please feel free to fork, pull request, discuss, share your ideas and raise issues. Any feedback is welcome!

ACKNOWLEDGEMENTS

Insipred by stream-to-mongo

LICENSE

MIT