<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[nodejs-learnings]]></title><description><![CDATA[nodejs-learnings]]></description><link>https://nodejs-learnings.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/685645bbd7e587715fb66bb3/850d0df9-670f-405b-a40b-a4a44e570514.png</url><title>nodejs-learnings</title><link>https://nodejs-learnings.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Dec 2026 00:01:16 GMT</lastBuildDate><atom:link href="https://nodejs-learnings.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[PATCH request gives 200 but not updating database (MongoDB)]]></title><description><![CDATA[I spent hours debugging a PATCH request that looked perfect but was not updating the database. this is the code i was sending the PATCH request from…]]></description><link>https://nodejs-learnings.hashnode.dev/patch-request-gives-200-but-not-mongodb</link><guid isPermaLink="true">https://nodejs-learnings.hashnode.dev/patch-request-gives-200-but-not-mongodb</guid><dc:creator><![CDATA[Aman Shahi]]></dc:creator><pubDate>Tue, 24 Mar 2026 17:57:56 GMT</pubDate><content:encoded><![CDATA[<p>I spent hours debugging a PATCH request that looked perfect but was not updating the database. this is the code</p>
<pre><code class="language-javascript">app.patch(async (req, res) =&gt; {
    let user = await User.findById(req.params.id);
    if(!user){
        return res.status(404).json({ message: "user not found"});    
    }

    Object.assign(user, req.body);

    await user.save();

    return res.status(200).json({
        user,
        message: "user updated successfully"
    })
})
</code></pre>
<p>i was sending the PATCH request from Postman using the key as last_name but mongoose schema has the key as lastName.<br />So the request was sent and i recieved a status 200 (All OK) but the field was not updating in the database.</p>
<p>then i debugged it for nearly 1-1.5 hours and got frustated then i opened postman and thought that let's change the key name and try it. and it worked!!!</p>
<p>i was using last_name (snake case) instead of lastName (camel case) because before using mongodb, i was using file system to store the data in memory and the field there was last_name.</p>
<p>but due to this i learnt a new thing about mongoose.</p>
<p>Here is my learning:</p>
<p>Mongoose documents are not just plain JS objects. They are special instances of a model.<br />When we use Object.assign(user, req.body) or user.set(req.body), then mongoose runs a process called "<strong>Internal Mapping</strong>"</p>
<p>here is how mongoose do this:<br />- It loops through the keys in req.body<br />- It checks if the key exists in our new mongoose.Schema({...})<br />- <strong>If it exists:</strong> It updates the value<br />- <strong>If it does not exists:</strong> It silently drops the key to prevent polluting your database with random data.</p>
<p>Best practices to follow so that we don't get into this mess:<br />- <strong>Always pick a standard</strong> (for e.g. use camalCase for everything or you can choose your own as well)<br />- <strong>Validation (extra safe):</strong> we can also add runValidators: true to our update calls to ensure new data still follows our schema rules (for e.g., unique: true for emails)<br />- <strong>Logging:</strong> always keep a console.log(req.body) at the top of your route while developing the backend. if you see the data in the console but not in the database then it's always a mismatch in the naming (like in my case: last_name and lastName)</p>
]]></content:encoded></item></channel></rss>