title: Join two fields MongoDB for report
body: <p>I have following collections:</p>
  
  <pre><code>&gt; db.website.find()
  { "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
  { "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com" }
  { "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" }
  { "_id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com" }
  { "_id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com" }
  { "_id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com" }
  { "_id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com" }
  { "_id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com" }
  { "_id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com" }
  { "_id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com" }
  </code></pre>
  
  <p>and this one:</p>
  
  <pre><code> db.websitegroup.find()
  { "_id" : "aws.amazon.com", "total" : 4, "hosting" : "aws.amazon.com" }
  { "_id" : "hostgator.com", "total" : 3, "hosting" : "hostgator.com" }
  { "_id" : "cloud.google.com", "total" : 2, "hosting" : "cloud.google.com" }
  { "_id" : "godaddy.com", "total" : 1, "hosting" : "godaddy.com" }
  </code></pre>
  
  <p>I want an .csv report with all the fields of website collection + total field of websitegroup, so I did the following in mongo console:</p>
  
  <pre><code>var joinTable = db.website.aggregate([{$lookup:{from:"websitegroup",localField:"hosting",foreignField:"hosting",as:"datosadjuntos"}}]);
  db.createCollection("joinTable");
  db.joinTable.insert(joinTable.toArray());
  </code></pre>
  
  <p>and in normal console I used mongoexport as it follows:</p>
  
  <pre><code>mongoexport --db test --collection joinTable --type=csv --out /home/joinTable_`date +"%d-%m-%y-%H:%M:%S"`.csv --fields _id,domainName,hosting,datosadjuntos.total
  </code></pre>
  
  <p>I get all the fields except for the datosadjuntos.total</p>
  
  <p><a href="https://i.stack.imgur.com/zOqPF.png" rel="nofollow"><img src="https://i.stack.imgur.com/zOqPF.png" alt="enter image description here"></a></p>
  
  <p>if I export it again but replacing datosadjuntos.total for just datosadjuntos.total like this:</p>
  
  <pre><code>mongoexport --db test --collection joinTable --type=csv --out /home/lavenec1/reportes_soyplus/joinTable_`date +"%d-%m-%y-%H:%M:%S"`.csv --fields _id,domainName,hosting,datosadjuntos
  </code></pre>
  
  <p>then I get this:</p>
  
  <p><a href="https://i.stack.imgur.com/qrHQA.png" rel="nofollow"><img src="https://i.stack.imgur.com/qrHQA.png" alt="enter image description here"></a></p>
  
  <p>it seems there's a [] surrounding each record...</p>
  
  <p>How to get this report with datosadjuntos.total correctly??</p>