<?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[Rohit Kumar's Tech Blog]]></title><description><![CDATA[Rohit Kumar's Tech Blog]]></description><link>https://blog.roht.me</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 19:46:09 GMT</lastBuildDate><atom:link href="https://blog.roht.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[MongoDB: Cannot do exclusion on field in inclusion projection]]></title><description><![CDATA[Cannot do exclusion on field `email` in inclusion projection 
code: 31254,

You may get this type of error in MongoDB while using mongoose, Although it's not mongoose-specific. It's actually a MongoDB error, basically, we can't include and exclude fi...]]></description><link>https://blog.roht.me/mongodb-cannot-do-exclusion-on-field-in-inclusion-projection</link><guid isPermaLink="true">https://blog.roht.me/mongodb-cannot-do-exclusion-on-field-in-inclusion-projection</guid><category><![CDATA[Cannot do exclusion on field 'password'  in inclusion projection ]]></category><category><![CDATA[code: 31254,]]></category><category><![CDATA[error code: 31254]]></category><category><![CDATA[MongoDB]]></category><category><![CDATA[Cannot do exclusion on field in inclusion projection ]]></category><dc:creator><![CDATA[Rohit Kumar]]></dc:creator><pubDate>Mon, 10 Oct 2022 19:53:36 GMT</pubDate><content:encoded><![CDATA[<pre><code class="lang-bash">Cannot <span class="hljs-keyword">do</span> exclusion on field `email` <span class="hljs-keyword">in</span> inclusion projection 
code: 31254,
</code></pre>
<p>You may get this type of error in MongoDB while using mongoose, Although it's not mongoose-specific. It's actually a MongoDB error, basically, we can't include and exclude fields simultaneously i.e at the same time<br />e.g.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> Client.findById(id).select(<span class="hljs-string">'name -email'</span>)
</code></pre>
<p>In this example, we want to include the <code>name</code> but exclude the <code>email</code> simultaneously. This is not possible hence the MongoDB throws an error.</p>
<p>But there is an exception here, According to <a target="_blank" href="https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/#suppress-_id-field">Official MongoDB</a> docs, only the <code>_id</code> field can be excluded while including other fields.<br />e.g.</p>
<pre><code class="lang-plaintext">const data = await Client.findById(id).select('name -_id')
</code></pre>
<p>This is perfectly fine.</p>
]]></content:encoded></item></channel></rss>