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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongo-river-elastic

v1.1.1

Published

mongodb to elastic river plugin, sync realtime

Readme

mongo-river-elastic

https://www.npmjs.com/package/mongo-river-elastic The mongo-river-elastic project provides a nodejs wrapper over mongodb nodejs , which synchronize data from your mongodb to elasticsearch. It is tested and works best with following:

  • Mongodb v4.0+
  • Mongodb Nodejs Driver v3.2
  • elasticsearch v6.0+ , v7.0+

Following Mongodb functions are supported:

Getting Started

Before you use the library, following points are crucial

Collections
  • It will not auto-create elasticsearch index, all the indexes has to be created by you.
  • All objects in a collection is appended with an extra key called river during insert/update and it is indexed. This key stores timestamp and is used for syncing with elasticsearch
  • To ensure consistency when elasticsearch is unreachable/down/busy, a mongodb collection river_backlog is created, which contains a single document having meta-information about objects yet to be synced. It will be deleted once elasticsearch is reachable and sync is complete.
Primary Key
  • Elasticsearch expects a primary key for each insert. You have to specify your primaryKeyField in mongodb document which will be used by the library.
  • If you dont have any primary key field, use mongodb bson id _id
  • Primary key in elasticsearch is always string.

Elasticsearch v7.0

  • This elasticsearch version has _type field depricated.
  • If your elasticsearch is v7.0+, library will ignore the type value provided in _collection_index_dict

Prerequisites

Dependent packages

npm install mongodb
npm install elasticsearch

Installing

npm install mongo-river-elastic
npm install mongo-river-elastic --save

Initialize

const River = require('mongo-river-elastic')
let river = new River(_mongo_db_ref, _es_ref, _collection_index_dict, options)

Parameters:

  • _mongo_db_ref   -- Object

    • mongodb database connection reference object
  • _es_ref   -- Object

    • elasticsearch connection reference object
  • _collection_index_dict   -- Object

    • Object dictionary that contains mapping between mongodb collections and elasticsearch index/type
    • Example
      _collection_index_dict =
      { 'collection1': {index: 'index1', type: 'type1', primaryKeyField: 'primaryKeyField1'},
        'collection2': {index: 'index2', type: 'type2', primaryKeyField: 'primaryKeyField2'}}
      where,
      • collection1: mongodb collection name
      • index1: elasticsearch index name
      • type: elasticsearch index mapping name. Set to null if not applicable.
      • primaryKeyField1: mongodb collection object key to be used as primary key in elasticsearch
  • options   -- Object

    • loglevel     String
      • Set log level for River logging (elasticsearch log)
      • info | error | debug
    • retryCount     number
      • No. of retries if elasticsearch write fails

Examples

Setup River Object
const elasticsearch = require('elasticsearch');
const MongoClient = require('mongodb').MongoClient;
const River = require('mongo-river-elastic');

// Connect Mongodb
MongoClient.connect('localhost:27017', function (err, mongoClient) {
    const _mongo_db_ref = mongoClient.db('test_database');

    // connect elasticsearch
    const _es_ref = new elasticsearch.Client({host: 'localhost:9200'});

    // init River
    const ._collection_index_dict = {
    'collection1': {index: 'index1', type: 'type1', primaryKeyField: '_id'},
    'collection2': {index: 'index2', type: 'type2', primaryKeyField: 'uuid'}
    };
    const options= {logLevel: 'debug', retryCount: 3};
    let river = new River(_mongo_db_ref, _es_ref, _collection_index_dict, options);
CRUDS
  • River CRUDS accepts options exactly similar to mongodb CRUDS
  • Only difference is, the first argument to any CRUD is name the of collection
  • mongodb_options is optional, similar to mongodb
  • INSERT
    const mongodb_options = {}
    //insert, insertOne, insertMany
    river.insertOne('collection1', {'_id': '1', 'key': 'value'}, mongodb_options, (err, response) => {
        ...
    });

    river.insertMany('collection2', [
        {'uuid': '1', 'key': 'value1'},
        {'uuid': '2', 'key': 'value2'},
        {'uuid': '3', 'key': 'value3'}], (err, response) => {
        ...
    });
  • UPDATE
 //update, updateOne, updateMany
    river.updateOne('collection1', {'key': 'value'}, {$set: {'anotherKey': 'anotherValue'}}, mongodb_options, (err, response) => {
        ...
    });
  • DELETE
    //deleteOne, deleteMany
    river.deleteOne('collection1', {'key': 'value'},mongodb_options, (err, response) => {
        ...
    });
  • REPLACE
    river.replaceOne('collection1', {'key': 'value'}, {
        '_id': 10,
        'key': 'value',
        'anotherKey': 'another value'
    }, (err, response) => {

    });
  • BULK WRITE
    river.bulkWrite('collection1',
    [
        {insertOne: {document: {'_id': '1', 'key': 'value'}}},
         {updateOne: {filter: {'key': 'value'},update: {$set: {'anotherKey': 'anotherValue'}},upsert: true}},
         {deleteOne: {filter: {'key': 'value'}}},
    ],mongodb_options, (err, response) => {

    });

License

This project is licensed under the MIT License