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

save-to-mongo

v1.0.0

Published

Save an object stream directly to MongoDB. Bulk modes supported (ordered / unordered) with configurable ops group size.

Downloads

4

Readme

Save-To-Mongo

A Node.js Writable stream useful for saving (big) object streams directly to MongoDB.

Features:

Usage

See /examples folder. But basically:

var JSONStream = require('JSONStream');
var fs = require('fs');
var path = require('path');

// init stream

var SaveToMongo = require('save-to-mongo');

var saveToMongo = SaveToMongo({
  uri: 'mongodb://127.0.0.1:27017/test',
  collection: 'savetomongo',
  bulk: {
    mode: 'unordered'
  }
});

// go!!!

fs.createReadStream(path.join(__dirname, './accounts.json'));
  .pipe(JSONStream.parse('*'))
  .pipe(saveToMongo)
  .on('execute-error', function(err) {
    console.log(err);
  })
  .on('done', function() {
    console.log('All done!');
    process.exit(0);
  });

JSON random data was generated using json-generator.

Documentation

To obtain a Save-To-Mongo instance, you have to do:

  var SaveToMongo = require('save-to-mongo');
  var stream = SaveToMongo({ /* options */ });

stream available options are:

  • uri (String, required): MongoDB connection uri.
  • collection (String, required): MongoDB target collection.
  • connection (Object, optional): optional connection options. You can pass all options supported by node-mongodb-native.
  • streamOptions (Object, optional): underlying Writable stream options. Default: { objectMode: true }.
  • bulk (Object, optional): enables bulk mode. If you set this, you may want to set also:
    • mode: (String, required): can be ordered or unordered. See MongoDB docs if you want more info on them.
    • bufferSize (Number, required): By default all bulk operations are splitted in groups, each contains 1000 ops. Change this if you want a lower value.

Events

"write-error"

Emitted every time a MongoDB insert op fails (e.g: duplicate _id). Note that this will not break the stream.

"execute-error"

Emitted every time a MongoDB execute op fails. Note that this will not break the stream.

"done"

Emitted when all writes are executed (after stream's finish event).

Performance

Both bulk modes (expecially unordered mode) increases significantly insert speed.

On provided examples:


# for 100 records

time node ./examples/default.js # real: 0m0.450s
time node ./examples/unordered.js # real: 0m0.340s

UPDATE: for 100000 records (similar to the ones used in examples), using bulk unordered method drop execution time from 2:42m to 1:42m. Not bad :)

Debug

This module is built with debug, so you can inspect what's happening.

On examples, you can do:

DEBUG=save-to-mongo node ./exampes/default.js

Install

With npm:

npm install save-to-mongo