Mongo to Elastic
Sync MongoDB to Elasticsearch
import { initSync, crate } from 'mongo2crate'
import { default as Redis } from 'ioredis'
import { MongoClient } from 'mongodb'
import retry from 'p-retry'
import elasticsearch from '@elastic/elasticsearch'
const client = await MongoClient.connect()
const db = client.db()
const elastic = new elasticsearch.Client()
const sync = initSync(new Redis(), db.collection('myCollection'), elastic, {
omit: ['password', 'unneededStuff'],
})
await sync.ignoreMalformed().catch(console.warn)
const changeStream = await sync.processChangeStream()
changeStream.start()
const schemaChange = await sync.detectSchemaChange(db)
schemaChange.start()
schemaChange.emitter.on('change', changeStream.stop)
const options = { batchSize: 1000 }
const initialScan = await sync.runInitialScan(options)
initialScan.start()