<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jai Aakash</title>
    <description>The latest articles on Forem by Jai Aakash (@jai_aakash_f1ce1de2d5ce13).</description>
    <link>https://forem.com/jai_aakash_f1ce1de2d5ce13</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3529960%2F3508cfee-8522-4854-bff7-c76c9ab0ad39.png</url>
      <title>Forem: Jai Aakash</title>
      <link>https://forem.com/jai_aakash_f1ce1de2d5ce13</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jai_aakash_f1ce1de2d5ce13"/>
    <language>en</language>
    <item>
      <title>How to perform numeric comparisons on string fields with units in MongoDB/Mongoose?</title>
      <dc:creator>Jai Aakash</dc:creator>
      <pubDate>Thu, 25 Sep 2025 17:40:49 +0000</pubDate>
      <link>https://forem.com/jai_aakash_f1ce1de2d5ce13/how-to-perform-numeric-comparisons-on-string-fields-with-units-in-mongodbmongoose-1igm</link>
      <guid>https://forem.com/jai_aakash_f1ce1de2d5ce13/how-to-perform-numeric-comparisons-on-string-fields-with-units-in-mongodbmongoose-1igm</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;I’m building a recipe API using Node.js, Express, and Mongoose. My MongoDB documents have nested nutrient fields stored as strings with units, for example:&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
  "nutrients": {&lt;br&gt;
    "calories": "389 kcal",&lt;br&gt;
    "proteinContent": "5 g",&lt;br&gt;
    "fatContent": "21 g"&lt;br&gt;
  },&lt;br&gt;
  "serves": "8 servings",&lt;br&gt;
  "title": "Sweet Potato Pie"&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;I want to allow clients to filter recipes by numeric ranges, e.g., calories less than 300 kcal.&lt;/p&gt;

&lt;p&gt;Problem:&lt;/p&gt;

&lt;p&gt;Since calories is stored as "389 kcal" (string), MongoDB cannot perform $gt, $lt, $gte, $lte operations correctly.&lt;/p&gt;

&lt;p&gt;I’m also storing serves as "8 servings" and other nutrients similarly with units.&lt;/p&gt;

&lt;p&gt;Questions:&lt;/p&gt;

&lt;p&gt;What is the best way to perform numeric comparisons for fields stored as strings with units?&lt;/p&gt;

&lt;p&gt;Should I migrate the database to store numeric values and units separately?&lt;/p&gt;

&lt;p&gt;If migration isn’t possible, how can I filter recipes efficiently — via aggregation or Node.js post-processing?&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>help</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
