<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Node.js - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/node-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sat, 09 Mar 2024 22:24:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>Node.js - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MongoDB Node.js to Replace String in Document</title>
		<link>https://thecodebuzz.com/mongodb-node-js-to-replace-string-update-substring-document/</link>
					<comments>https://thecodebuzz.com/mongodb-node-js-to-replace-string-update-substring-document/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 06 Jan 2024 14:34:42 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[MongoDB Node.js to Replace String]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=29408</guid>

					<description><![CDATA[<p>MongoDB Node.js to Replace String in Document Today this article will see how to write a query using MongoDB Node.js to replace string or substring In a Document We will replace a string in the Mongo document with the input string using a simple and easy approach i.e using $replaceOne aggregation operator which is supported [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/mongodb-node-js-to-replace-string-update-substring-document/">MongoDB Node.js to Replace String in Document</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">MongoDB Node.js to Replace String in Document</h1>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" width="1024" height="310" src="https://www.thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-1024x310.jpg" alt="#image_title #separator_sa #post_seo_title" class="wp-image-29401" style="width:739px;height:auto" srcset="https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-1024x310.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-300x91.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-768x232.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-1536x464.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-785x237.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell.JPG 1548w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today this article will see how to write a query using MongoDB Node.js to replace string or substring In a Document</p>



<p></p>



<p>We will replace a string in the Mongo document with the input string using a simple and easy approach i.e using  <strong><em><code>$replaceOne</code> </em></strong>aggregation operator which is supported in MongoDB v4.0 onwards.</p>



<p></p>



<p></p>



<p>We will cover below aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting started -MongoDB Node.js to Replace String</a></li><li><a href="#aioseo-mongo-field-length-query-using-cli-or-compass-ui">Node.js &#8211; Replace Substring in MongoDB using replaceOne Aggregator</a><ul><li><a href="#aioseo-what-is-replaceone">What is $replaceOne</a></li></ul></li><li><a href="#aioseo-query-pattern-replace-using-replaceone">Query Pattern Replace using $replaceOne</a></li><li><a href="#aioseo-example-using-replaceone">Node.js Example using $replaceOne</a></li></ul></div>



<p></p>



<p>We already looked at a simple way of adding or updating a new field to the document in our previous&nbsp;<strong><a href="https://www.thecodebuzz.com/mongodb-add-new-field-every-document-in-mongodb-collection/" target="_blank" rel="noreferrer noopener">MongoDB sample series</a>.</strong></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting started -MongoDB Node.js to Replace String</h2>



<p></p>



<p>Here below is a sample schema or document we shall use,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="1024" height="508" src="https://www.thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1024x508.jpg" alt="" class="wp-image-29398" style="width:813px;height:auto" srcset="https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1024x508.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-300x149.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-768x381.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1536x762.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-785x390.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document.jpg 1926w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<pre class="wp-block-code"><code>
</code></pre>



<h2 class="wp-block-heading" id="aioseo-mongo-field-length-query-using-cli-or-compass-ui">Node.js &#8211; Replace Substring in MongoDB using replaceOne Aggregator </h2>



<p></p>



<p>We are going to use a newly introduced aggregate operator to replace easily a string with matching criteria with part of the string.</p>



<p></p>



<div data-schema-only="false" class="wp-block-aioseo-faq" id="aioseo-what-is-replaceone"><h3 class="aioseo-faq-block-question">What is <code>$replaceOne</code></h3><div class="aioseo-faq-block-answer">
<p><a href="https://www.mongodb.com/docs/upcoming/reference/operator/aggregation/replaceOne/#mongodb-expression-exp.-replaceOne" target="_blank" rel="noopener" title=""><code>$replaceOne</code></a> is aggregation operator which helps replace the first instance of a search string in an input string with a replacement string.</p>



<p></p>



<p>This operator usage&nbsp;is <em>case-sensitive</em>.</p>



<p></p>
</div></div>



<h2 class="wp-block-heading" id="aioseo-query-pattern-replace-using-replaceone">Query Pattern Replace using $replaceOne</h2>



<p></p>



<pre class="wp-block-code has-medium-font-size"><code>{ 
$replaceOne: { 
    input: &lt;expression&gt;, 
    find: &lt;expression&gt;, 
    replacement: &lt;expression&gt;}
}
</code></pre>



<p>In the above query,</p>



<p></p>



<figure class="wp-block-table is-style-stripes"><table><tbody><tr><td>input</td><td>Input string with search criteria.</td></tr><tr><td>find</td><td>Find the document with matching input string criteria.</td></tr><tr><td>replacement</td><td>The string to use to replace the first matched instance from input</td></tr></tbody></table></figure>



<p></p>



<p>You can replace the existing Mongo field using any other fields from the document using the above approach + using the approach discussed <a href="https://www.thecodebuzz.com/mongodb-add-padding-logic-with-prefix-or-suffix-examples/" target="_blank" rel="noopener" title="MongoDB – Add Padding logic with prefix or suffix examples">here</a>.</p>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-example-using-replaceone">Node.js Example using $replaceOne</h2>



<p></p>



<p>In the below example, we will replace &#8220;total-order&#8221; with &#8220;order&#8221; in the mongo field.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="1024" height="508" src="https://www.thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1-1024x508.jpg" alt="" class="wp-image-29403" style="width:840px;height:auto" srcset="https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1-1024x508.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1-300x149.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1-768x381.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1-1536x762.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1-785x390.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-1.jpg 1926w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Please add below using namespace </p>



<pre class="wp-block-code"><code>import { MongoClient } from 'mongodb';

</code></pre>



<p>Define the Regex query for the matching string,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
&#039;Link&#039;: {     
     &#039;$regex&#039;: new RegExp(&#039;total-order&#039;)   
 }
</pre></div>


<p></p>



<p>Define the Set using <strong>replaceOne </strong>Operator,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&#039;$set&#039;: {
      &#039;Link&#039;: {
        &#039;$replaceOne&#039;: {
          &#039;input&#039;: &#039;$Link&#039;, 
          &#039;find&#039;: &#039;total-order&#039;, 
          &#039;replacement&#039;: &#039;order&#039;
        }
      }
    }
</pre></div>


<p></p>



<p><strong>Example</strong></p>



<pre class="wp-block-code"><code>
const filter = {
  'Link': {
    '$regex': new RegExp('total-order')
  }
},&#91;
  {
    '$set': {
      'Link': {
        '$replaceOne': {
          'input': '$Link', 
          'find': 'total-order', 
          'replacement': 'order'
        }
      }
    }
  }
];
const client = await MongoClient.connect(
  'mongodb://host/',
  { useNewUrlParser: true, useUnifiedTopology: true }
);

const coll = client.db('TheCodeBuzz').collection('Orders');
const cursor = coll.find(filter);
const result = await cursor.toArray();
await client.close();</code></pre>



<p></p>



<p>Below is the schema post-update,</p>



<p></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="600" height="299" src="https://www.thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-query.jpg" alt="Replace Substring in MongoDB document" class="wp-image-29399" srcset="https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-query.jpg 600w, https://thecodebuzz.com/wp-content/uploads/2023/12/Replace-Substring-in-MongoDB-document-shell-query-300x150.jpg 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></figure>



<p>That&#8217;s all! Happy coding!</p>



<p></p>



<p>Does this help you fix your issue? </p>



<p></p>



<p>Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/mongodb-node-js-to-replace-string-update-substring-document/">MongoDB Node.js to Replace String in Document</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongodb-node-js-to-replace-string-update-substring-document/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Node.js &#8211; MongoDB Date Time query with examples</title>
		<link>https://thecodebuzz.com/node-js-mongodb-date-time-query-node-mongodb-native/</link>
					<comments>https://thecodebuzz.com/node-js-mongodb-date-time-query-node-mongodb-native/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 19 Nov 2023 00:24:16 +0000</pubDate>
				<category><![CDATA[MongoDB and Mongoose]]></category>
		<category><![CDATA[Node.js]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=28930</guid>

					<description><![CDATA[<p>Node &#8211; MongoDB Date Time query with examples Today, in this article will cover Node.js &#8211; MongoDB Date Time query with examples. We will see how to get records using a date filter like &#8216;greater than&#8217; OR &#8216;less than&#8217; or get the records between 2 dates. We will use the MongoDB Node.js Driver package to [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/node-js-mongodb-date-time-query-node-mongodb-native/">Node.js – MongoDB Date Time query with examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">Node &#8211; MongoDB Date Time query with examples</h1>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="315" src="https://www.thecodebuzz.com/wp-content/uploads/2023/11/node-js-MongoDB-Date-Time-query-with-examples-1024x315.jpg" alt="Node.js - MongoDB Date Time query with examples-mongoose" class="wp-image-28935" srcset="https://thecodebuzz.com/wp-content/uploads/2023/11/node-js-MongoDB-Date-Time-query-with-examples-1024x315.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/11/node-js-MongoDB-Date-Time-query-with-examples-300x92.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/11/node-js-MongoDB-Date-Time-query-with-examples-768x236.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/11/node-js-MongoDB-Date-Time-query-with-examples-785x241.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2023/11/node-js-MongoDB-Date-Time-query-with-examples.jpg 1340w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today, in this article will cover Node.js &#8211; MongoDB Date Time query with examples. </p>



<p></p>



<p>We will see how to get records using a date filter like &#8216;greater than&#8217; OR &#8216;less than&#8217; or get the records between 2 dates.</p>



<p></p>



<p>We will use the <em><em>MongoDB Node.js Driver</em></em> <em>package</em> to install the Mongo driver and query MongoDB.</p>



<p>For more details &#8211; <em>https://mongodb.github.io/node-mongodb-native</em></p>



<p></p>



<p></p>



<p>We will cover the below aspects in today&#8217;s article,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting started</a></li><li><a href="#aioseo-install-pymongo-pcakge">Install Node.js MongoDB driver</a></li><li><a href="#aioseo-add-pymongo-import-statements">Add MongoDB Import statements</a></li><li><a href="#aioseo-mongo-date-query-using-cli-or-compass-ui">Define MongoDB Connection</a></li><li><a href="#aioseo-date-greater-than-query">Node.js- Mongo Date greater than query (&#x27;gt&#x27;)</a><ul><li><a href="#aioseo-define-the-date">Define the date</a></li><li><a href="#aioseo-define-the-filter">Define the filter</a></li><li><a href="#aioseo-get-the-results">Get the results</a></li><li><a href="#aioseo-below-is-the-complete-code">Complete sample code,</a></li><li><a href="#aioseo-mongo-atlas-ui">Mongo Atlas UI</a></li></ul></li><li><a href="#aioseo-node-js-mongo-date-less-than-query-lt">Node js- Mongo Date less than query (&#x27;lt&#x27;)</a></li><li><a href="#aioseo-node-js-mongo-date-less-than-and-greater-than-date-query">Node js- Mongo Date less than and greater than date query</a></li></ul></div>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting started</h2>



<p></p>



<p>Here below is a sample schema or document we shall use for the date range query,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="700" height="878" src="https://www.thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id.jpg" alt="Node js mongodb date query" class="wp-image-12234" srcset="https://thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id.jpg 700w, https://thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id-239x300.jpg 239w, https://thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id-415x520.jpg 415w" sizes="auto, (max-width: 700px) 100vw, 700px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-install-pymongo-pcakge">Install Node.js MongoDB driver</h2>



<p></p>



<p>Please install the Node.js MongoDB driver package to get support for the MongoClient connection class and properties. </p>



<p></p>



<p>For more details visit- <a href="https://mongodb.github.io/node-mongodb-native" target="_blank" rel="noopener" title=""> <em>https://mongodb.github.io/node-mongodb-native</em></a></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-add-pymongo-import-statements">Add MongoDB Import statements </h2>



<p></p>



<p>Please add below using namespaces </p>



<p></p>



<pre class="wp-block-preformatted">import { MongoClient } from 'mongodb';</pre>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-mongo-date-query-using-cli-or-compass-ui">Define MongoDB Connection</h2>



<p></p>



<p>Using MongoClient class define the MongoDB Connection</p>



<p></p>



<pre class="wp-block-code"><code>const client = await MongoClient.connect(
  'mongodb://your server:27017/',
  { useNewUrlParser: true, useUnifiedTopology: true }
);</code></pre>



<p></p>



<p>Here we will be using the below query to get the documents between two dates in MongoDB Collection.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-date-greater-than-query">Node.js- Mongo Date greater than query (&#8216;gt&#8217;)</h2>



<p></p>



<h4 class="wp-block-heading" id="aioseo-define-the-date">Define the date </h4>



<p></p>



<pre class="wp-block-code"><code>const queryDate = new Date('2023-11-01');
</code></pre>



<p></p>



<h4 class="wp-block-heading" id="aioseo-define-the-filter">Define the filter </h4>



<p></p>



<p>Lets define the date filter as below,</p>



<p></p>



<pre class="wp-block-code"><code>const filter = {
  'DateAdded': {
    '$gt': queryDate

  }
};


</code></pre>



<h4 class="wp-block-heading" id="aioseo-get-the-results">Get the results </h4>



<p></p>



<pre class="wp-block-code"><code>const coll = client.db('TheCodeBuzz').collection('Books');

const cursor = coll.find(filter);

const result = await cursor.toArray();

await client.close();</code></pre>



<p></p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-below-is-the-complete-code">Complete sample code,</h4>



<p></p>



<pre class="wp-block-code"><code>import { MongoClient } from 'mongodb';

# Define Node Mongo Connection

const client = await MongoClient.connect(
  'mongodb://your server:27017/',
  { useNewUrlParser: true, useUnifiedTopology: true }
);

# define date for the query 

const queryDate = new Date('2023-11-01');

# define filter for the query 

const filter = {
  'DateAdded': {
    '$gt': queryDate

  }
};


const coll = client.db('TheCodeBuzz').collection('Books');
const cursor = coll.find(filter);
const result = await cursor.toArray();
await client.close();


</code></pre>



<p></p>



<h4 class="wp-block-heading" id="aioseo-mongo-atlas-ui"><strong>Mongo Atlas UI</strong></h4>



<p></p>



<p>The above query produces the same result as indicated below using ATLAS UI,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="433" src="https://www.thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-1024x433.jpg" alt="node js datetime mongodb format, node.js isodate, node  insert datetime now, node mongodb date time query, " class="wp-image-13546" srcset="https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-1024x433.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-300x127.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-768x325.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-1536x650.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-785x332.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass.jpg 1921w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-node-js-mongo-date-less-than-query-lt">Node js- Mongo Date less than query (&#8216;lt&#8217;)</h2>



<p></p>



<p>Let&#8217;s define Node.js Mongo Datetime less than query (&#8216;lt&#8217;)</p>



<p></p>



<pre class="wp-block-code"><code># define date for the query 


const queryDate = new Date('2023-11-01');

# define filter for the query 

const filter = {
  'DateAdded': {
    '$lt': queryDate

  }
};</code></pre>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-node-js-mongo-date-less-than-and-greater-than-date-query">Node js- Mongo Date less than and greater than date query </h2>



<p></p>



<p></p>



<pre class="wp-block-code"><code># define date for the query 

const queryStartDate = new Date('2023-11-01');
const queryEndDate = new Date('2024-01-01');


# define filter for the query 

filter =  {
         'DateAdded': {
                '$gte': queryStartDate,
                '$lt': queryEndDate

         }
  }</code></pre>



<p>For more details on queries where no date field in the schema is available , please visit this article,</p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/query-mongodb-objectid-by-date-mongoshell-compass/" target="_blank" rel="noopener" title="Query MongoDB using ObjectId by Date -MongoShell">Query MongoDB using ObjectId by Date -MongoShell</a></li>
</ul>



<p></p>



<p></p>



<p>That&#8217;s all! Happy coding!</p>



<p></p>



<p>Does this help you fix your issue? </p>



<p></p>



<p>Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/node-js-mongodb-date-time-query-node-mongodb-native/">Node.js – MongoDB Date Time query with examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/node-js-mongodb-date-time-query-node-mongodb-native/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MongoDB nested array string field value length query</title>
		<link>https://thecodebuzz.com/mongodb-nested-array-string-field-value-length-query/</link>
					<comments>https://thecodebuzz.com/mongodb-nested-array-string-field-value-length-query/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 25 Jan 2023 05:31:04 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[mongodb field length limit]]></category>
		<category><![CDATA[mongodb filter by length of string]]></category>
		<category><![CDATA[mongodb length of string greater than]]></category>
		<category><![CDATA[mongodb length of string in array]]></category>
		<category><![CDATA[mongodb nested array length]]></category>
		<category><![CDATA[mongodb nested array of objects]]></category>
		<category><![CDATA[mongodb sort by length of string]]></category>
		<category><![CDATA[mongodb string length less than]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=25957</guid>

					<description><![CDATA[<p>MongoDB nested array string field value length query Today this article will see how to write a MongoDB nested array string field value length query. Today in this article, we will cover below aspects, We will cover below query types, We shall see examples for MongoDB field query where we will get the records based [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/mongodb-nested-array-string-field-value-length-query/">MongoDB nested array string field value length query</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">MongoDB nested array string field value length query</h1>



<figure class="wp-block-image size-large is-resized"><a href="https://www.thecodebuzz.com/mongodb-string-field-value-length-query-greater-than/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="1024" height="292" src="https://www.thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-1024x292.jpg" alt="MongoDB string field value length query,mongodb length of string greater than,
mongodb filter by length of string,
mongodb length of string in array,
<a href=" class="wp-image-13556" srcset="https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-1024x292.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-300x86.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-768x219.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-1536x438.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-785x224.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query.jpg 1633w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Today this article will see how to write a MongoDB nested array string field value length query.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting started</a></li><li><a href="#aioseo-mongo-field-length-query-using-cli-or-compass-ui">Mongo field length query using Mongo Shell or Compass UI</a><ul><li><a href="#aioseo-mongodb-string-field-value-length-greater-than-query">MongoDB nested array string field value greater than query ></a></li><li><a href="#aioseo-mongodb-string-field-value-length-less-than-query">MongoDB nested Array field string value length less than query <</a></li><li><a href="#aioseo-mongodb-string-field-value-length-equal-to-length">MongoDB nested Array field string value length Equal to length =</a></li></ul></li><li><a href="#aioseo-using-regex">Using Regex &#8211; MongoDB nested array string field value length query</a></li></ul></div>



<p></p>



<p>We will cover below query types,</p>



<p></p>



<ul class="wp-block-list">
<li>Greater than field value length </li>
</ul>



<ul class="wp-block-list">
<li>Less than field value length </li>
</ul>



<ul class="wp-block-list">
<li>Equal to the length of the field value</li>
</ul>



<p></p>



<p>We shall see examples for MongoDB field query where we will get the records based on field length value greater than or less than of specified value. <span style="font-size: revert; color: initial;">We will run field-length queries</span> using CLI or  Compass UI<span style="font-size: revert; color: initial;">.</span></p>



<p></p>



<p>We already looked at a simple way of adding or updating a new field to the document in our previous <strong><a href="https://www.thecodebuzz.com/mongodb-add-new-field-every-document-in-mongodb-collection/" target="_blank" rel="noreferrer noopener">MongoDB sample series</a>. </strong></p>



<p></p>



<p>We shall also see how to create a similar query using other languages like C# mongo driver.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting started</h2>



<p></p>



<p>Here below is a sample schema or document we shall use for array object with field length query and will cover less than or greater than length query for field value &#8220;Books.category.value&#8221; field.</p>



<p></p>



<p>Below by value field is of type string and we will get all records where such field length is less than or greater than the specified size.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="783" src="https://www.thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-greater-than-less-than-1024x783.jpg" alt="" class="wp-image-25959" srcset="https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-greater-than-less-than-1024x783.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-greater-than-less-than-300x229.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-greater-than-less-than-768x587.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-greater-than-less-than-680x520.jpg 680w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-mongo-field-length-query-using-cli-or-compass-ui">Mongo field length query using Mongo Shell or Compass UI </h2>



<p></p>



<p>Here we will be using the below query to get the documents based on the value of the length of fields in the MongoDB Collection.</p>



<p>Nested array element finding and then getting its length can be done easily using below simple query as explained.</p>



<p></p>



<p><strong><em>Pattern</em>:</strong></p>



<p></p>



<pre id="block-0c76aa9e-4113-436b-874c-263dc8417774" class="wp-block-preformatted has-medium-font-size">{
  "$expr": {
    "$anyElementTrue": {
      "$map": {
        "input": {
          "$reduce": {
            "input": "$&lt;YourFieldName(fully qualified field)&gt;",
            "initialValue": [],
            "in": { "$concatArrays": [ "$$&lt;YourFieldName&gt;", "$$this" ] }
          }
        },
        "in": { "$gte": [ { "$strLenCP": "$$this" }, &lt;size of array&gt;] }
      }
    }
  }
}
</pre>



<p></p>



<p id="block-5a4646dd-384f-4d1f-acaa-faefc9a3a739">Where &lt;YourFieldName&gt; is the Mongo Field Name whose value length needs to be calculated.  </p>



<p></p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-mongodb-string-field-value-length-greater-than-query">MongoDB nested array string field value greater than query &gt;</h3>



<p></p>



<p>Below is the MongoDB string field value length greater than the query.</p>



<p><strong><em>Example</em></strong></p>



<p></p>



<p>In the below query, we get records where the field exists and the field length is <em><strong>greater than 5</strong></em> characters in size.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="730" src="https://www.thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-less-than-1024x730.jpg" alt="mongodb update field with another field value " class="wp-image-25961" srcset="https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-less-than-1024x730.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-less-than-300x214.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-less-than-768x547.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/01/MongoDB-nested-array-field-value-length-query-less-than-729x520.jpg 729w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h3 class="wp-block-heading" id="aioseo-mongodb-string-field-value-length-less-than-query">MongoDB nested Array field string value length less than query &lt;</h3>



<p></p>



<p>Below is the mongo Shell query for less than dates.</p>



<p></p>



<p><strong><em>Query Pattern</em></strong></p>



<p></p>



<p>Replace in the above query,</p>



<p></p>



<pre id="block-fbc452bd-ec31-4685-8b79-db63c13f0bcc" class="wp-block-preformatted has-medium-font-size"> "in": { "$lte": [ { "$strLenCP": "$$this" }, &lt;size of array&gt;] }</pre>



<p> </p>



<h3 class="wp-block-heading" id="aioseo-mongodb-string-field-value-length-equal-to-length">MongoDB nested Array field string value length Equal to length =</h3>



<p></p>



<p></p>



<p>Below is the mongo Shell query for equal to length query.</p>



<p></p>



<p><strong><em>Query Pattern</em></strong></p>



<p></p>



<p>Replace in the above query,</p>



<p></p>



<pre class="wp-block-preformatted"> "in": { "$eq": [ { "$strLenCP": "$$this" }, &lt;size of array&gt;] }</pre>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-using-regex">Using Regex &#8211; MongoDB nested array string field value length query</h2>



<p></p>



<p>For more details on this approach please visit: </p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/regex-mongodb-array-string-field-value-length-greater-than-less-than/" title="Using Regex – MongoDB array string field value length query"><strong>Using Regex – MongoDB array string field value length query</strong></a></li>
</ul>



<p></p>



<p></p>



<div class="wp-block-comments">
<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>
</div>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/mongodb-nested-array-string-field-value-length-query/">MongoDB nested array string field value length query</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongodb-nested-array-string-field-value-length-query/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MongoDB &#8211; Query for documents array size is greater than</title>
		<link>https://thecodebuzz.com/mongodb-query-for-documents-array-size-is-greater-than-mongoshell-cli-node-js/</link>
					<comments>https://thecodebuzz.com/mongodb-query-for-documents-array-size-is-greater-than-mongoshell-cli-node-js/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 18 May 2022 04:53:00 +0000</pubDate>
				<category><![CDATA[MongoDB and Mongoose]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[mongo shell array length]]></category>
		<category><![CDATA[mongodb compass array size greater than]]></category>
		<category><![CDATA[mongodb count array elements]]></category>
		<category><![CDATA[mongodb get size of array in document]]></category>
		<category><![CDATA[mongodb length of array greater than]]></category>
		<category><![CDATA[mongodb size with condition]]></category>
		<category><![CDATA[mongodb-query array size greater than 0]]></category>
		<category><![CDATA[size mongodb aggregation]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22694</guid>

					<description><![CDATA[<p>MongoDB Query for documents array size is greater than Today in this article we will see how to perform MongoDB &#8211; Query for documents array size greater than 1 or more. Recently I was trying to find all the records where the array field has more than 1 entry. After the initial trivial, I found [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/mongodb-query-for-documents-array-size-is-greater-than-mongoshell-cli-node-js/">MongoDB – Query for documents array size is greater than</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>MongoDB Query for documents array size is greater than</strong></h1>



<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/mongodb-query-for-documents-array-size-is-greater-than-mongoshell-cli-node-js" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="1024" height="304" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-documents-array-size-is-greater-than-1-1024x304.jpg" alt="MongoDB - Query for documents array size is greater than" class="wp-image-22722" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-documents-array-size-is-greater-than-1-1024x304.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-documents-array-size-is-greater-than-1-300x89.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-documents-array-size-is-greater-than-1-768x228.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-documents-array-size-is-greater-than-1-1536x456.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-documents-array-size-is-greater-than-1-785x233.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-documents-array-size-is-greater-than-1.jpg 1570w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Today in this article we will see how to perform  MongoDB &#8211; Query for documents array size greater than 1 or more.</p>



<p></p>



<p>Recently I was trying to find all the records where the array field has more than 1 entry. </p>



<p></p>



<p>After the initial trivial, I found there is no direct query but was able to find a query that helped me address this requirement easily.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-query-pattern">Approach I &#8211; MongoDB Query using the not-equal operator</a></li><li><a href="#aioseo-approach-ii--mongodb-query-for-documents-array-size-is-greater-than-0-or-1-or-2">Approach II &#8211; MongoDB Query for documents array size is greater than 0 or 1 or 2</a></li></ul></div>



<p></p>



<p>Below is a sample JSON where array order is defined as an array type.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
{
  &quot;_id&quot;: &quot;617f1b826cbcd5cf388e44f7&quot;,
  &quot;Ssn&quot;: {
    &quot;$binary&quot;: &quot;AQN8wobDt3M8SsOPwrjDsHNVw5Ymwo3DlQJ1AQpYwrhyw6oIwp/CgAzCpCHDqcKBwrDCjTo+w4/CsMO0wozDmmfCuxE7w4tywofDoR/DhMOgw6HDhsKZwoRLwovCu8KDXQTCncKbL11nwpRQBMKeTcKLFlXCvwYxwrxUwoo=&quot;,
    &quot;$type&quot;: &quot;0&quot;
  },
  &quot;City&quot;: &quot;NY&quot;,
  &quot;Country&quot;: &quot;USA&quot;,
  &quot;States&quot;: &quot;NY&quot;,
  &quot;Zip&quot;: &quot;12345&quot;,
  &quot;order&quot;:  &#x5B; &quot;name1&quot;,  &quot;name2&quot;, &quot;name3&quot; ],
  &quot;Name&quot;: &quot;John&quot;
}
</pre></div>


<p></p>



<p>Array <strong><em>order </em></strong>has 1 to 3 items in the collection. </p>



<p>We will build a query to find all the items where the array size is greater than 1.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-query-pattern">Approach I &#8211; MongoDB Query using the not-equal operator</h2>



<p></p>



<p><strong>Command</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{&lt;array field&gt;: {$type:'array', $ne: [] }}</pre>



<p></p>



<p><strong>Example</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{order: {$type: 'array', $ne: []}}</pre>



<p></p>



<p></p>



<p>The above query confirms if the array element has<em><strong> at least 1 or more</strong></em> records.</p>



<p></p>



<p>In our above example, The below query gives us results for array size <strong><em>1 or 2</em></strong> as below,</p>



<p></p>



<figure class="wp-block-image size-full is-resized"><a href="https://www.thecodebuzz.com/mongodb-query-for-documents-array-size-is-greater-than-mongoshell-cli-node-js" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="702" height="686" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-1-2.jpg" alt="" class="wp-image-22717" style="width:589px;height:576px" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-1-2.jpg 702w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-1-2-300x293.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-1-2-532x520.jpg 532w" sizes="auto, (max-width: 702px) 100vw, 702px" /></a></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-approach-ii--mongodb-query-for-documents-array-size-is-greater-than-0-or-1-or-2">Approach II &#8211; MongoDB Query for documents array size is greater than 0 or 1 or 2</h2>



<p></p>



<p><strong>Query </strong>&#8211;  MongoDB array documents with a count greater than1 </p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{'order<span class="has-inline-color has-vivid-red-color">.0</span>': {$exists: true}}</pre>



<p></p>



<p>The above query will return all the records where the <strong><em>array size is greater than or equal to 1 </em></strong></p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="480" height="686" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-0-or-1.jpg" alt="query-for-documents-where-array-size-is-greater-than" class="wp-image-22720" style="width:240px;height:343px" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-0-or-1.jpg 480w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-0-or-1-210x300.jpg 210w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-0-or-1-364x520.jpg 364w" sizes="auto, (max-width: 480px) 100vw, 480px" /></figure>



<p></p>



<p><strong>Get MongoDB array documents with a count greater than 2</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{'order.<span class="has-inline-color has-vivid-red-color">1</span>': {$exists: true}}

</pre>



<p> The above query will return all the records where the <strong><em>array size is greater than or equal to 2 </em></strong></p>



<p></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="392" height="494" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-2.jpg" alt="query-for-documents-where-array-size-is-greater-than" class="wp-image-22721" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-2.jpg 392w, https://thecodebuzz.com/wp-content/uploads/2022/07/MongoDB-Query-for-documents-array-size-is-greater-than-2-238x300.jpg 238w" sizes="auto, (max-width: 392px) 100vw, 392px" /></figure>



<p></p>



<p></p>



<p><strong><em>References:</em></strong></p>



<p></p>



<ul class="wp-block-list">
<li><em><a href="https://www.thecodebuzz.com/mongodb-query-case-sensitive-case-insensitive/" target="_blank" rel="noreferrer noopener" title="MongoDB Case sensitive query examples"><strong>MongoDB Case-sensitive query examples</strong></a></em></li>
</ul>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/mongodb-c-driver-net-core-examples-getting-started/" target="_blank" rel="noreferrer noopener" title="Getting Started MongoDB in .NET Core with Examples"><em><strong>Getting Started MongoDB in .NET Core with Examples</strong></em></a></li>
</ul>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/mongodb-query-for-documents-array-size-is-greater-than-mongoshell-cli-node-js/">MongoDB – Query for documents array size is greater than</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongodb-query-for-documents-array-size-is-greater-than-mongoshell-cli-node-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Add Swagger OpenAPI in Node.js and Express</title>
		<link>https://thecodebuzz.com/swagger-openapi-documentation-node-js-and-express-api/</link>
					<comments>https://thecodebuzz.com/swagger-openapi-documentation-node-js-and-express-api/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 06 Jul 2020 03:47:00 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[add swagger to node js]]></category>
		<category><![CDATA[node js express swagger auto generate]]></category>
		<category><![CDATA[node js swagger auto generate]]></category>
		<category><![CDATA[swagger in node js]]></category>
		<category><![CDATA[Swagger in Node.js and Express API]]></category>
		<category><![CDATA[swagger integration with node js express]]></category>
		<category><![CDATA[swagger-jsdoc]]></category>
		<category><![CDATA[swagger-ui-express example]]></category>
		<category><![CDATA[swagger-ui-express npm]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=11207</guid>

					<description><![CDATA[<p>Add Swagger OpenAPI in Node.js and Express Today in this article, we shall see how to Add Swagger OpenAPI in Node.js and Express API. Swagger&#160;or&#160;OpenAPI&#160;describes the standards and specifications for the RESTFul API description. These specifications are an attempt to create a universal and language agnostic description for describing the REST API. In this article, [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/swagger-openapi-documentation-node-js-and-express-api/">Add Swagger OpenAPI in Node.js and Express</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">Add Swagger OpenAPI in Node.js and Express</h1>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="979" height="442" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API.jpg" alt="Add Swagger OpenAPI in Node.js and Express" class="wp-image-11218" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API.jpg 979w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-300x135.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-768x347.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-785x354.jpg 785w" sizes="auto, (max-width: 979px) 100vw, 979px" /></figure>



<p>Today in this article, we shall see how to Add Swagger OpenAPI in Node.js and Express API. <strong>Swagger</strong>&nbsp;or&nbsp;<strong>OpenAPI&nbsp;</strong>describes the standards and specifications for the RESTFul API description. </p>



<p></p>



<p>These specifications are an attempt to create a universal and language agnostic description for describing the REST API.</p>



<p></p>



<p>In this article, we shall see below overall aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started-add-swagger-openapi-in-node-js">Getting started &#8211; Add Swagger OpenAPI in Node.js</a></li><li><a href="#aioseo-please-enable-the-swagger-module-in-the-api-pipeline">Please enable the Swagger module in the API pipeline,</a></li><li><a href="#aioseo-lets-define-a-simple-get-api">Let&#8217;s define a simple GET API</a></li><li><a href="#aioseo-add-api-level-global-metadata">Add API level Global metadata</a></li><li><a href="#aioseo-add-swagger-ui-setup">Add Swagger UI Setup</a></li><li><a href="#aioseo-launch-swagger-url">Launch Swagger URL</a></li><li><a href="#aioseo-add-rest-method-level-description">Add REST method level description</a><ul><li><a href="#aioseo-summary">Summary</a></li></ul></li><li><a href="#aioseo-"></a></li></ul></div>



<p></p>



<p>Node.js express API can use <em><strong>OpenAPI V3.0</strong></em>&nbsp;specifications which are more generic guidelines and specifications around how the API documentation should be generated.</p>



<p>Swagger provides the advantage of understanding and consuming the RESTFule API (for developers ).</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started-add-swagger-openapi-in-node-js">Getting started &#8211; Add Swagger OpenAPI in Node.js</h2>



<p></p>



<p>Add Swagger OpenAPI in Node.js is easy and can be performed using below high-level steps discussed,</p>



<p></p>



<p>Create your Node + Express project and perform NPM install.</p>



<p></p>



<p>Install node package called swagger-ui-express using NPM as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">npm install swagger-ui-express --save
</pre>



<p></p>



<p>Install node package called swagger-jsdoc using NPM as below,</p>



<p></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">npm install swagger-jsdoc --save
</pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-please-enable-the-swagger-module-in-the-api-pipeline">Please enable the Swagger module in the API pipeline,</h2>



<p></p>



<p>Enable Swagger module&#8217;s in the API pipeline in <em><strong>App.js </strong></em>as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">const swaggerUi = require("swagger-ui-express");

const swaggerJsondoc = require("swagger-jsdoc");
</pre>



<h2 class="wp-block-heading" id="aioseo-lets-define-a-simple-get-api">Let&#8217;s define a simple GET API </h2>



<p></p>



<p>Let&#8217;s define a simple GET API that returns a string as a response to the request.</p>



<p></p>



<p>Add below GET method in the <em><strong>App.js </strong></em>as below,</p>



<p> </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">

app.get(&quot;/hello&quot;, (req, res) =&gt; {
  res.json(&quot;Hello world!&quot;);
});


</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-add-api-level-global-metadata">Add API level Global metadata</h2>



<p></p>



<p>API level global data can be defined using <strong><em>Info </em></strong>object with details like Title, Version, or Description details.</p>



<p>Below is the list of API level documentation elements that can be specified,</p>



<p></p>



<ul class="wp-block-list"><li><strong>Title </strong></li></ul>



<ul class="wp-block-list"><li><strong>Version</strong></li></ul>



<ul class="wp-block-list"><li><strong>Description</strong></li></ul>



<ul class="wp-block-list"><li><strong>Contact</strong></li></ul>



<ul class="wp-block-list"><li><strong>License details </strong></li></ul>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
const options = {
  swaggerDefinition: {
    openapi: &quot;3.0.0&quot;,
    info: {
      title: &quot;TheCodebuzz API&quot;,
      version: &quot;1.0.0&quot;,
      description:
        &quot;Thecodebuzz test service to demo how to document your API&quot;,
      license: {
        name: &quot;MIT&quot;,
        url: &quot;https://www.thecodebuzz.com&quot;
      },
      contact: {
        name: &quot;TheCodeBuzz&quot;,
        url: &quot;https://www.thecodebuzz.com&quot;,
        email: &quot;info@thecodebuzz.com&quot;
      }
    },
    servers: &#x5B;
      {
        url: &quot;http://localhost:3000/&quot;
      }
    ]
  },
  apis: &#x5B;]
};

</pre></div>


<p> </p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-add-swagger-ui-setup">Add Swagger UI Setup </h2>



<p></p>



<p>Add Swagger UI as middleware in the express API pipeline.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
const specs = swaggerJsondoc(options);
app.use(&quot;/docs&quot;, swaggerUi.serve);


app.get(
  &quot;/docs&quot;,
  swaggerUi.setup(specs, {
    explorer: true
  })
);

</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-launch-swagger-url">Launch Swagger URL </h2>



<p></p>



<p>Swagger URL can be launched here by using <a href="http://localhost:3000/docs">http://localhost:3000/docs</a></p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="331" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/Swagger-express-Node-JS-API-documentation-1024x331.jpg" alt="Swagger Node js" class="wp-image-11211" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/Swagger-express-Node-JS-API-documentation-1024x331.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/Swagger-express-Node-JS-API-documentation-300x97.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/Swagger-express-Node-JS-API-documentation-768x249.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/Swagger-express-Node-JS-API-documentation-1536x497.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/06/Swagger-express-Node-JS-API-documentation-785x254.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/Swagger-express-Node-JS-API-documentation.jpg 1582w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-add-rest-method-level-description">Add REST method level description</h2>



<p></p>



<p>Let&#8217;s define <strong><em>api-method-docs.js</em></strong> which contains the REST method description as per the supported schema of <em><strong>Swagger/OpenAPI v3.0.</strong></em></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
/**
 * @swagger
 * tags:
 *   name: Hello
 *   description: Greetings API from TheCodeBUzz
 */

/**
 * @swagger
 * path:
 *  /hello:
 *    get:
 *      summary: Get greeting message from TheCodebuzz
 *      responses:
 *        &quot;200&quot;:
 *          description: GET reponse from API
 *          content:
 *            application/json:
 *              schema:
 *                type: string
 */


</pre></div>


<p></p>



<p>Above API level description can be defined in the <em><strong>api-method-doc.js</strong></em> as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">  apis: ["./ api-method-doc.js"]</pre>



<p></p>



<p>Please see below updated data,</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
const options = {
  swaggerDefinition: {
    openapi: &quot;3.0.0&quot;,
    info: {
      title: &quot;TheCodebuzz API&quot;,
      version: &quot;1.0.0&quot;,
      description:
        &quot;Thecodebuzz test service to demo how to document your API&quot;,
      license: {
        name: &quot;MIT&quot;,
        url: &quot;https://www.thecodebuzz.com&quot;
      },
      contact: {
        name: &quot;TheCodeBuzz&quot;,
        url: &quot;https://www.thecodebuzz.com&quot;,
        email: &quot;infoATthecodebuzz.com&quot;
      }
    },
    servers: &#x5B;
      {
        url: &quot;http://localhost:3000/&quot;
      }
    ]
  },
  apis: &#x5B;&quot;./api-doc.js&quot;]
};

</pre></div>


<p></p>



<p>Please relaunch your API with the above changes.</p>



<p>Once launched, you shall see the GET method level API description below,</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="450" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-documentation-1024x450.jpg" alt="Add Swagger Express API" class="wp-image-11212" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-documentation-1024x450.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-documentation-300x132.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-documentation-768x338.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-documentation-785x345.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-documentation.jpg 1458w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Let&#8217;s execute the GET API from Swagger UI. You see Swagger GET API returns your expected result.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="595" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-description-1024x595.jpg" alt="Add Swagger (OpenAPI) to Node.js and Express API" class="wp-image-11213" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-description-1024x595.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-description-300x174.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-description-768x447.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-description-785x456.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/OpenAPI-Swagger-express-Node-JS-API-description.jpg 1510w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>That&#8217;s All.</p>



<p></p>



<p>Now you can use the above technique to add API documentation for POST, PUT, DELETE methods as required.</p>



<p></p>



<p><strong><em>References:</em></strong></p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/angular-architecture-and-design-guidelines/" target="_blank" rel="noreferrer noopener" title="Angular Architecture and Design Guidelines"><strong><em>Angular Architecture and Design Guidelines</em></strong></a></li></ul>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/swagger-openapi-3-0-sample-json-example-jwt-basic-auth/" target="_blank" rel="noreferrer noopener" title="Swagger 3.0 example (OpenApi 3.0 sample example)"><strong><em>Swagger 3.0 example (OpenApi 3.0 sample example</em></strong>)</a></li></ul>



<p></p>



<p></p>



<p>That&#8217;s all! Happy coding!</p>



<p></p>



<p>Does this help you fix your issue? </p>



<p></p>



<p>Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-summary">Summary</h4>



<p></p>



<p>Its simple to enable Swagger OpenAPI documentation in the Node js. Good API documentation helps reduce dependencies and provides the advantage of understanding the REST API(for developers) consuming API. It provides easy-ready API documentation and providing details of the capabilities a service/API provides.</p>



<p> </p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-"> </h2>



<p></p><p>The post <a href="https://thecodebuzz.com/swagger-openapi-documentation-node-js-and-express-api/">Add Swagger OpenAPI in Node.js and Express</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/swagger-openapi-documentation-node-js-and-express-api/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
