<?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>MongoExport - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/mongoexport/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Tue, 03 Oct 2023 20:59:20 +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>MongoExport - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Resolved: mongoexport error &#8211; is not valid JSON query</title>
		<link>https://thecodebuzz.com/mongoexport-is-not-valid-json-query-cli-csv-json-export/</link>
					<comments>https://thecodebuzz.com/mongoexport-is-not-valid-json-query-cli-csv-json-export/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 20 Nov 2022 23:09:00 +0000</pubDate>
				<category><![CDATA[MongoExport]]></category>
		<category><![CDATA[failed: error parsing query as extended json: invalid json input]]></category>
		<category><![CDATA[mongoexport and mongoimport]]></category>
		<category><![CDATA[mongoexport query filter example]]></category>
		<category><![CDATA[mongoexport query is not valid json.mongoexport query objectid]]></category>
		<category><![CDATA[mongoexport vs mongodump]]></category>
		<category><![CDATA[mongoexport with date query]]></category>
		<category><![CDATA[mongoexport with uri]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=24690</guid>

					<description><![CDATA[<p>mongoexport error &#8211; is not a valid JSON query Issue Description Executing mongoexport CLI utility gives the below error, error validating settings: query &#8221; is not valid JSON: invalid character &#8216;2&#8217; after top-level value Resolution mongoexport&#160;is a CLI *command-line interface) a tool that creates a JSON or CSV export of data stored in a MongoDB [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/mongoexport-is-not-valid-json-query-cli-csv-json-export/">Resolved: mongoexport error – is not valid JSON query</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">mongoexport error &#8211; is not a valid JSON query</h1>



<h2 class="wp-block-heading">Issue Description</h2>



<p></p>



<p>Executing mongoexport CLI utility gives the below error,</p>



<p></p>



<p class="has-vivid-red-color has-text-color has-medium-font-size">error validating settings: query &#8221; is not valid JSON: invalid character &#8216;2&#8217; after top-level value</p>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" src="//thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2022/11/mongoexport-issue-with-json-query-extended-json-invalid-json-inpu-1-1024x221.jpg" alt="mongoexport error - is not valid JSON query" class="wp-image-24692"/></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading">Resolution</h2>



<p></p>



<p><strong><code>mongoexport</code>&nbsp;</strong>is a CLI *command-line interface) a tool that creates a JSON or CSV export of data stored in a MongoDB database.</p>



<p></p>



<p>It&#8217;s important to run this command from the system command line and not from the <strong>mongo </strong>shell.</p>



<p><br>While using mongoexport , please follow the below guidelines while using the<strong> &#8211;query</strong> option in the commands.</p>



<p></p>



<ul class="wp-block-list">
<li>Mongoexport requires a query option to send the query as a JSON document i.e query with enclosed documents.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>The query&nbsp;<strong>must</strong>&nbsp;be in&nbsp;Extended JSON v2 format ie. either relaxed or canonical/strict mode with enclosing the field names and operators in quotes:</li>
</ul>



<p></p>



<p>Considering I had below sample query to be run with export commands,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">'{"DateAdded":{"$gt":ISODate('2022-01-01T00:00:00.000+00:00')}}'</pre>



<p>while using the above query, I got the below error,</p>



<p></p>



<p class="has-vivid-red-color has-text-color has-link-color wp-elements-dd77fe32a00ec03a85f060995b650bce">mongoexport.exe &#8211;uri=mongodb://xxxx:27017/TheCodeBuzz?readPreference=primary -q &#8216;{&#8220;DateAdded&#8221;:{&#8220;$gt&#8221;:ISODate(&#8216;2022-01-01T00:00:00.000+00:00&#8242;)}}&#8217; &#8211;out=C://opt/test6.csv &#8211;type=csv &#8211;collection=Books &#8211;fields=UserId,DateAdded<br>2022-11-27T16:38:27.330-0500 WARNING: ignoring unsupported URI parameter &#8216;readpreference&#8217;<br>2022-11-27T16:38:29.385-0500 error validating settings: query &#8216;[39 123 68 97 116 101 65 100 100 101 100 58 123 36 103 116 58 73 83 79 68 97 116 101 40 39 50 48 50 50 45 48 49 45 48 49 84 48 48 58 48 48 58 48 48 46 48 48 48 43 48 48 58 48 48 39 41 125 125 39]&#8217; is not valid JSON: invalid character &#8216;2&#8217; after top-level value<br>2022-11-27T16:38:29.385-0500 try &#8216;mongoexport &#8211;help&#8217; for more information</p>



<p></p>



<p></p>



<p>I was able to resolve the issue below two approaches,</p>



<p></p>



<h2 class="wp-block-heading">Approach 1 </h2>



<p></p>



<p>I was able to fix the issue by swapping the double quotes as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"> "{'DateAdded':{'$gt':ISODate('2022-01-01T00:00:00.000+00:00')}}"
</pre>



<p></p>



<h2 class="wp-block-heading">Approach 2 </h2>



<p></p>



<p>I was also able to run all complex queries using double quotes as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">"{\"DateAdded\":{\"$gt\":ISODate('2022-01-01T00:00:00.000+00:00')}}"</pre>



<p></p>



<p>Using either of the above approaches results were successful,</p>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" src="//thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2022/11/mongodb-query-output-to-file-csv-and-json-mongoexport-utility-1024x220.jpg" alt="" class="wp-image-24693"/></figure>



<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>The post <a href="https://thecodebuzz.com/mongoexport-is-not-valid-json-query-cli-csv-json-export/">Resolved: mongoexport error – is not valid JSON query</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongoexport-is-not-valid-json-query-cli-csv-json-export/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MongoDB- How to Redirect Output of Mongo query to a file ( CSV or JSON)</title>
		<link>https://thecodebuzz.com/redirect-output-of-mongodb-query-to-a-file-export-mongoose-csv-and-json/</link>
					<comments>https://thecodebuzz.com/redirect-output-of-mongodb-query-to-a-file-export-mongoose-csv-and-json/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 07 Nov 2022 18:50:00 +0000</pubDate>
				<category><![CDATA[MongoExport]]></category>
		<category><![CDATA[export data to csv from mongodb by using java]]></category>
		<category><![CDATA[mongodb compass export query result to csv]]></category>
		<category><![CDATA[mongodb export query result to json]]></category>
		<category><![CDATA[mongodb query output format]]></category>
		<category><![CDATA[mongodb query output to file]]></category>
		<category><![CDATA[mongoexport query filter example]]></category>
		<category><![CDATA[mongoexport query is not valid json]]></category>
		<category><![CDATA[redirect-output-of-mongodb-query-to-a-file-export-mongoose-csv-and-jsomongodb export query result to csv python]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=24669</guid>

					<description><![CDATA[<p>MongoDB- How to Redirect Output of Mongo query to a file ( CSV or JSON) Today in this article, we shall see how to Redirect Output of Mongo query to a file ( CSV or JSON ) using available tools like MongoExport CLI tools. You may have required MongoDB query which returns the records collection [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/redirect-output-of-mongodb-query-to-a-file-export-mongoose-csv-and-json/">MongoDB- How to Redirect Output of Mongo query to a file ( CSV or JSON)</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">MongoDB- How to Redirect Output of Mongo query to a file ( CSV or JSON)</h1>



<figure class="wp-block-image size-large"><img decoding="async" src="//thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2022/11/mongodb-query-output-to-filemongoexport-query-filter-example-1-1024x327.jpg" alt="mongodb query output to file, mongoexport query filter example" class="wp-image-24683"/></figure>



<p>Today in this article, we shall see how to Redirect Output of Mongo query to a file ( CSV or JSON ) using available tools like MongoExport CLI tools.</p>



<p>You may have required MongoDB query which returns the records collection using needed criteria.</p>



<p></p>



<p>If you need to run Mongoquery and want to store the mongodb query output to file like JSON or CSV file, then please follow the below explained steps. </p>



<p></p>



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



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-using-cli-tool-mongoexport-to-export-the-data-as-csv-or-json">Using the CLI tool mongoexport to redirect query output CSV or JSON</a></li><li><a href="#aioseo-mongocompass-ui-export-documents-as-csv-or-json">MongoCompass UI &#8211; get output from query within UI</a><ul><li><a href="#aioseo-step1-open-the-collection-and-click-on-the-export-collection-icon-as-shown-below">Step1: Open the collection and click on the Export Collection icon</a></li><li><a href="#aioseo-step-2-select-an-option-to-export-using-a-filter-query-you-can-select-only-fields-of-your-choice">Step 2:  Select an option to export using a filter query. you can select only fields of your choice.</a></li><li><a href="#aioseo-step-3-select-the-fields-that-need-to-be-downloaded-in-the-target-exported-file">Step 3:  Select the fields that need to be downloaded in the target exported file</a></li><li><a href="#aioseo-step-4-please-select-the-options-json-or-csv-as-required">Step 4: Please select the options JSON or CSV as required.</a></li></ul></li></ul></div>



<p>We shall see how to export the MongoDB collection as CSV or JSON using Compass UI and&nbsp;<strong><em>mongoexport&nbsp;</em></strong>utility tools available.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-using-cli-tool-mongoexport-to-export-the-data-as-csv-or-json"><strong>Using the CLI tool mongoexport to redirect query output CSV or JSON</strong></h2>



<p></p>



<p>Using the CLI tool&nbsp;<strong>mongoexport&nbsp;</strong>to export the data as CSV or JSON</p>



<p></p>



<p id="block-3331047b-fb8d-4dbb-be67-70197834852c"><strong>mongoexport&nbsp;</strong>utility can be found at the below location,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><strong><em>C:\Program Files\MongoDB\Server.0\bin</em></strong>
</pre>



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



<p></p>



<pre id="block-21b4b4d9-a312-4672-a4fa-1f3703ddfb84" class="wp-block-preformatted has-medium-font-size">mongoexport.exe --uri="mongodb uri" --collection="collection name" --type=csv -q 'MongoDb query" --fields=fields1,field2,field3 --out = "file name"
</pre>



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



<p></p>



<ul class="wp-block-list"><li><strong>&#8211;uri </strong>  &#8211;  Specify the hostname and port in the</li></ul>



<p></p>



<ul class="wp-block-list"><li><strong>&#8211;collection</strong>  &#8211;   Specifies the collection to export.</li></ul>



<p></p>



<ul class="wp-block-list"><li><strong>&#8211;db</strong>   &#8211; Specifies the name of the database</li></ul>



<p></p>



<ul class="wp-block-list"><li><strong>&#8211;fields</strong>  &#8211; Specifies a field or fields to&nbsp;<em>include</em>&nbsp;in the export. To specify multiple fields, use a comma-separated list of fields.</li></ul>



<p></p>



<ul class="wp-block-list"><li><strong>&#8211;out</strong>   &#8211;  Specifies a file name to which to write the data returned from the query.</li></ul>



<p></p>



<ul class="wp-block-list"><li><code>--<strong>query or -q</strong></code> &#8211;  <a href="https://www.mongodb.com/docs/database-tools/mongoexport/#std-option-mongoexport.--query"></a>provides a query as a&nbsp;JSON document&nbsp;(enclosed in quotes) to return matching documents in the export.</li></ul>



<p></p>



<p>Specifies query as a&nbsp;JSON document&nbsp;to return matching documents in the export. The query should be enclosed (enclosed in quotes).</p>



<p></p>



<p>For more details on all the commands options please <a href="https://www.mongodb.com/docs/database-tools/mongoexport/#options" target="_blank" rel="noopener" title="">visit</a></p>



<p></p>



<p><strong><em>Example</em></strong> <strong>Mongo <em>Query</em></strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{"DateAdded":{"$gt":ISODate('2022-01-01T00:00:00.000+00:00')}}</pre>



<p></p>



<p>Let&#8217;s execute the above query as below using the MongoExport utility,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><strong>mongoexport</strong>.exe --uri= mongodb://xxxx:xxxx/TheCodeBuzz?readPreference=primary --collection=Books  <span class="has-inline-color has-luminous-vivid-orange-color">-q "{\"DateAdded\":{\"$gt\":ISODate('2022-01-01T00:00:00.000+00:00')}}"</span> --type=csv --out=C://opt/sample.csv --fields=UserId,DateAdded
</pre>



<p>Above we are running a date range query where we are trying to get all the records where &#8220;DateAdded&#8221; is greater than the 2022 year from Mongo.</p>



<p></p>



<p>We are exporting a CSV for 2 fields i.e <em><strong> UserId, DateAdded </strong></em></p>



<p></p>



<p>Generated CSV file looks as below,</p>



<p></p>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" src="//thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2022/11/redirect-output-of-mongo-query-to-a-csv-file.jpg" alt="Redirect Output of MongoDB query to a CSV file,mongodb query output to file,
mongodb compass export query result to csv.
mongodb export query result to csv python,
mongoexport query filter example,
mongodb export query result to json,
robo 3t export query result to csv,
mongoexport query is not valid json,
export data to csv from mongodb by using java,
" class="wp-image-24670" width="604" height="356" srcset="https://thecodebuzz.com/wp-content/uploads/2022/11/redirect-output-of-mongo-query-to-a-csv-file.jpg 926w, https://thecodebuzz.com/wp-content/uploads/2022/11/redirect-output-of-mongo-query-to-a-csv-file-300x177.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/11/redirect-output-of-mongo-query-to-a-csv-file-768x453.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/11/redirect-output-of-mongo-query-to-a-csv-file-785x463.jpg 785w" sizes="(max-width: 604px) 100vw, 604px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-mongocompass-ui-export-documents-as-csv-or-json"><strong>MongoCompass UI &#8211; get output from query within UI</strong></h2>



<p></p>



<p>If you are using <strong>MongoCompass </strong>UI or Robo mongo client, both have rich UI features which let you redirect the query output to documents like CSV or JSON as required.</p>



<p></p>



<p><strong>Steps to redirect the query</strong> <strong><em>as below,</em></strong></p>



<p></p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-step1-open-the-collection-and-click-on-the-export-collection-icon-as-shown-below">Step1: Open the collection and click on the Export Collection icon</h3>



<p></p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="//thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2022/11/redirect-mongo-query-results-to-file.JPG" alt="mongodb query output to file,
mongodb compass export query result to csv.
mongodb export query result to csv python,
mongoexport query filter example,
mongodb export query result to json,
robo 3t export query result to csv,
mongoexport query is not valid json,
export data to csv from mongodb by using java,
" class="wp-image-24674" width="627" height="343" srcset="https://thecodebuzz.com/wp-content/uploads/2022/11/redirect-mongo-query-results-to-file.JPG 722w, https://thecodebuzz.com/wp-content/uploads/2022/11/redirect-mongo-query-results-to-file-300x164.jpg 300w" sizes="(max-width: 627px) 100vw, 627px" /></figure>



<p></p>



<h3 class="wp-block-heading" id="aioseo-step-2-select-an-option-to-export-using-a-filter-query-you-can-select-only-fields-of-your-choice"><strong>Step </strong>2:  Select an option to export using a filter query. you can select only fields of your choice.</h3>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="//thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2022/11/mongodb-query-output-to-file-csv-and-json-1024x540.jpg" alt="mongodb compass export query result to csv" class="wp-image-24676" width="612" height="322"/></figure>



<p></p>



<h3 class="wp-block-heading" id="aioseo-step-3-select-the-fields-that-need-to-be-downloaded-in-the-target-exported-file">Step 3:  Select the fields that need to be downloaded in the target exported file</h3>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="//thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2022/11/redirect-output-of-mongo-query-how-to-select-field-1024x636.jpg" alt="mongodb export query result to json" class="wp-image-24677" width="592" height="367"/></figure>



<h3 class="wp-block-heading" id="aioseo-step-4-please-select-the-options-json-or-csv-as-required">Step 4: Please select the options JSON or CSV as required. </h3>



<p></p>



<p>You need to provide an output file path. Next click on the&nbsp;<strong>EXPORT&nbsp;</strong>button.</p>



<p></p>



<figure class="wp-block-image is-resized"><img loading="lazy" decoding="async" src="https://thecodebuzz-1d620.kxcdn.com/wp-content/uploads/2021/08/export-mongodb-as-CSV-or-JSON-1024x576.jpg" alt="mongodb output as CSV or JSON" class="wp-image-17678" width="604" height="339" title="export-mongodb-as-CSV-or-JSON | TheCodeBuzz" srcset="https://thecodebuzz.com/wp-content/uploads/2021/08/export-mongodb-as-CSV-or-JSON-1024x576.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/08/export-mongodb-as-CSV-or-JSON-300x169.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/08/export-mongodb-as-CSV-or-JSON-768x432.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/08/export-mongodb-as-CSV-or-JSON-785x442.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/08/export-mongodb-as-CSV-or-JSON.jpg 1237w" sizes="auto, (max-width: 604px) 100vw, 604px" /></figure>



<p></p>



<p>After successful export, you shall see a generated CSV or JSON file with the required details.</p>



<p></p>



<p><strong><em>Reference :</em></strong></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/mongodb-distributed-transaction-acid-csharp-driver-aspnetcore/" target="_blank" rel="noreferrer noopener"><strong><em>MongoDB Distributed Transaction – Guidelines</em></strong></a></li></ul>



<p></p>



<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>



<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>



<p></p><p>The post <a href="https://thecodebuzz.com/redirect-output-of-mongodb-query-to-a-file-export-mongoose-csv-and-json/">MongoDB- How to Redirect Output of Mongo query to a file ( CSV or JSON)</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/redirect-output-of-mongodb-query-to-a-file-export-mongoose-csv-and-json/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
