jbdemonte / mongoose-elasticsearch-xp

A mongoose plugin that indexes models into Elasticsearch 2 / 5 / 6 and 7
93 stars 34 forks source link

Why new records not indexed to ES automatically? #62

Open cyberz opened 4 years ago

cyberz commented 4 years ago

OS: MacOS 10.15 Catalina Elasticsearch: 6.8.4 MongoDB: 4.0.3 Mongoose: 5.5.0

A'm already have collection with N records and according documentation I indexed it:

Car.esSynchronize().then(function () { console.log('end.') });

All fine, N records was added to ES index.

Car.esSearch() gives me results: { took: 9, timed_out: false, _shards: { total: 5, successful: 5, skipped: 0, failed: 0 }, hits: { total: 2019, max_score: 0, hits: ...

Next I insert/update records in mongodb collection Car:

Car.findOneAndUpdate({ carId }, { $set: data }, { upsert: true, setDefaultsOnInsert: true, useFindAndModify: false });

But nothing inserted/updated automatically in ES!

What I'm doing wrong ?

GaldanM commented 4 years ago

Hi @cyberz,

As mentionned in the README, you have to use the option { new: true } in order for findOneAndUpdate function to work correctly with ES.