Jekyll2020-09-02T02:25:23+00:00https://www.ravdeep.in/feed.xmlCuriosityPersonal WebsiteRavdeep PasrichaImporting records into MongoDB2015-07-22T00:00:00+00:002015-07-22T00:00:00+00:00https://www.ravdeep.in/importing-records-into-mongodb<p>In this tutorial, I will demonstrate how we can use <code class="language-plaintext highlighter-rouge">mongoimport</code> to quickly add records(or documents) into <code class="language-plaintext highlighter-rouge">MongoDB</code> from JSON or CSV file. We will walk through different structures of file to import documents from.</p>
<h3 id="json">JSON</h3>
<p>Consider a file <code class="language-plaintext highlighter-rouge">employee.json</code> which contains the employee’s information, each line represents a single document(or record), as shown below:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> { name:"John", city: "New York", state: "NY" }
{ name:"Miller", city: "Dallas", state: "TX" }
{ name:"James", city: "Los Angeles", state: "CA" }
</code></pre></div></div>
<p>Now to import these records into our local <code class="language-plaintext highlighter-rouge">MongoDB</code>, just run:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> mongoimport --db <db_name> --collection <collection_name> --file employee.json
</code></pre></div></div>
<p>Default file type used by <code class="language-plaintext highlighter-rouge">mongoimport</code> is <code class="language-plaintext highlighter-rouge">JSON</code>, so we don’t need to explicitly specify the <code class="language-plaintext highlighter-rouge">--type</code> here.</p>
<h3 id="json-array">JSON Array</h3>
<p>Let’s consider the same data as in above example(employee.json) but in JSONArray format as shown below:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> [
{ name:"John", city: "New York", state: "NY" },
{ name:"Miller", city: "Dallas", state: "TX" },
{ name:"James", city: "Los Angeles", state: "CA" }
]
</code></pre></div></div>
<p>To import these records into our local <code class="language-plaintext highlighter-rouge">MongoDB</code>, just use <code class="language-plaintext highlighter-rouge">--jsonArray</code> parameter:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> mongoimport --db <db_name> --collection <collection_name> --file employee.json --jsonArray
</code></pre></div></div>
<h3 id="csv-with-headerline">CSV with headerline</h3>
<p>Let’s consider the same data as above in CSV format(employee.csv):</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> "Name", "City", "State"
"John", "New York", "NY"
"Miller", "Dallas", "TX"
"James", "Los Angeles", "CA"
</code></pre></div></div>
<p>Use the below command to import these records and specify <code class="language-plaintext highlighter-rouge">--headerline</code> parameter, so that <code class="language-plaintext highlighter-rouge">mongoimport</code> determines the fields from the first line:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> mongoimport --db <db_name> --collection <collection_name> --type csv --headerline --file employee.csv
</code></pre></div></div>
<h3 id="csv-without-headerline">CSV without headerline</h3>
<p>Let’s take the same csv file from above example but without the headerline.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> "John", "New York", "NY"
"Miller", "Dallas", "TX"
"James", "Los Angeles", "CA"
</code></pre></div></div>
<p>To specify the fields use <code class="language-plaintext highlighter-rouge">--fields</code> or <code class="language-plaintext highlighter-rouge">-f</code> option followed by comma sperated field names.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> mongoimport --db <db_name> --collection <collection_name> --type csv --file employee.csv --fields name,city,state
</code></pre></div></div>
<p>or alternatively we can also use <code class="language-plaintext highlighter-rouge">--fieldFile</code> option, which allows to specify the file which contains fields name.
<br />
<br /></p>
<h3 id="import-json-to-remote-host">Import JSON to remote Host</h3>
<p>In above examples we have considered that <code class="language-plaintext highlighter-rouge">MongoDB</code> is running locally. If we want to import records into MongoDB instance running on some remote host, we can use the following command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> mongoimport --host <host_address> --port <port_no> --db <db_name> --collection <coll_name> --file employee.json
</code></pre></div></div>
<p>There are a lot more options in <code class="language-plaintext highlighter-rouge">mongoimport</code>, which I have not covered in this blog. Please refer to <a href="http://docs.mongodb.org/manual/reference/program/mongoimport/">mongoimport</a> documentation for more information.</p>
<p><br />
Thank you for reading.</p>Ravdeep PasrichaIn this tutorial, I will demonstrate how we can use mongoimport to quickly add records(or documents) into MongoDB from JSON or CSV file. We will walk through different structures of file to import documents from.