mongoosastic findOneAndUpdate not indexing in elasticsearch

允我心安 提交于 2019-12-13 02:12:18

问题


Problem : In my example if i user schema.save then it will indexed in elastic search

but problem starts when i use findOneAndUpdate so it will not index in elastic even if i insert (i.e save)

MovieSchema.findOneAndUpdate(query, reqObject, {
        upsert: true
    }, function(err, results) {
        if (err) {
            if (!update) {
                 var filePath = path.join(__dirname, "../../movie/images/uploads/") + reqObject.imageUrl;
                 fs.unlinkSync(filePath);
             }
            console.log(err)
            callback({
                RESULT_CODE: '-1',
                MESSAGE: 'System error. Please try again'
            });
        } else {

            callback({
                RESULT_CODE: '1',
                MESSAGE: 'Movie inserted'
            });

        }
    });


回答1:


I fond the answer using findOneAndUpdate

Example:

NOTE : pass new : true option upsert: true and it will work

New option will return updated or created object so internally mongoosastic work like if inserted object or updated object return then onty it will insert in elastic search index

MovieSchema.findOneAndUpdate(query, reqObject, {
        upsert: true,'new': true
    }, function(err, results) {
        if (err) {
            callback({
                RESULT_CODE: '-1',
                MESSAGE: 'System error. Please try again'
            });
        } else {

            callback({
                RESULT_CODE: '1',
                MESSAGE: 'Movie inserted'
            });

        }
    });



来源:https://stackoverflow.com/questions/33992359/mongoosastic-findoneandupdate-not-indexing-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!