findOneAndUpdate not updating value in mongodb?

47 Views Asked by At

im new to nodejs and i was just wants to update integer in mongodb but i con't do so. i dont understand what im doing wrong here.no matter what i try findAndUpdate only returning original document without updating it even on mongodb atlus the document is unaltered. I also tried typecasting integer value but still didn't work.

const id = "6606ff9e1a5c69a06612219b"
// const objid = new mongoose.Types.ObjectId(id)
export const UpdateMoisture = async (req, res) => {
    try {
        const moist = req.body.moisture
        const update = {$set:{"moisture":parseInt(moist)}}
        const result = await Sensor_data.findOneAndUpdate(
            {_id:id},
            update,
            {
                new:true
            }
        )
        console.log(result)
        // Return the updated data
        return res.status(200).json({ result, success: true });
    } catch (error) {
        // Log and return error response
        console.error('Error while updating moisture:', error);
        return res.status(500).json({ msg: 'Error while updating moisture data', error: error, success: false });
    }
}```

Schema

import mongoose from 'mongoose'

const Sensordataschema = new mongoose.Schema({
    relay: {
        type: Boolean,
        default:false,
    },
    temperature:{
        type: Number,
    },
    moisture:{
        type:Number,
    }
    // Add more fields as needed for your specific use case
});

const Sensor_data = mongoose.model('sensor datas', Sensordataschema);

export default Sensor_data

0

There are 0 best solutions below