Reputation: 139
I am using MongoDB, Mongoose, Elasticsearch and Mongoosastic on a Node project. I have a MongoDB Atlas database and a local Elasticsearch database which are mapped together. When I create a new Document in MongoDB it is created in ES as well, and when I delete it in Mongo it is deleted in ES too. So everything works until this point. What I did next is, I added an update route to update specific documents in Mongo. They do get updated but the changes are not reflected in ES because I might be missing something. Does anyone have any ideas?
Here is the Model/Schema in donation.js
:
const mongoose = require('mongoose');
const mongoosastic = require('mongoosastic');
const Schema = mongoose.Schema;
const donationSchema = new Schema({
donorUsername: {
type: String,
required: true,
es_indexed:true
},
bankName: {
type: String,
required: true,
es_indexed:true
},
qualityChecked: {
type: String,
required: true,
es_indexed:true
},
usedStatus: {
type: String,
required: true,
es_indexed:true
},
}, { timestamps: true });
donationSchema.plugin(mongoosastic, {
"host": "localhost",
"port": 9200
});
const Donation = mongoose.model('Donation', donationSchema , 'donations');
Donation.createMapping((err, mapping) => {
console.log('mapping created');
});
module.exports = Donation;
Here are the create and update functions/routes in donationController.js
:
const donation_create_post = (req, res) => {
console.log(req.body);
const donation = new Donation(req.body);
donation.save()
.then(result => {
res.redirect('/donations');
})
.catch(err => {
console.log(err);
});
}
const donation_update = (req, res) => {
const filter = { donorUsername: req.body.donorUsername };
const update = { bankName: req.body.bloodbankName,
qualityChecked: req.body.qualityChecked,
usedStatus: req.body.usedStatus };
Donation.findOneAndUpdate(filter, update)
.then(result => {
res.redirect('/donations');
})
.catch(err => {
console.log(err);
});
}
Upvotes: 1
Views: 772
Reputation: 139
Solved it. Added {new: true}
in Donation.findOneAndUpdate(filter, update, {new: true})
.
Upvotes: 1