title: How to use the substr function in mongodb?
body: <p>This is my solution to export data from mongo into a table into oracle, if there's a better way, I'd appreciate that as well.</p>
  
  <p>My current query is:</p>
  
  <pre><code>db.getCollection('table_name').find({"key.specificKey": "value"})
  </code></pre>
  
  <p>What I want to do is select a specific substring to display. I tried using the substr function as found in the <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/substr/" rel="noreferrer">mongodb docs</a>. However this wasn't working for me.</p>
  
  <pre><code>db.getCollection('table_name').aggregate({columnName: {$substr: ["$key", 0, 2]}})
  </code></pre>
  
  <p>I also tried using the match function as suggested <a href="https://stackoverflow.com/questions/25436630/mongodb-how-to-find-and-then-aggregate">here</a>. But that was also not working.</p>
  
  <pre><code>db.getCollection('table_name').aggregate($match: {"key.specificKey": "value"}, {columnName: {$substr: ["$key", 0, 2]}})
  </code></pre>
  
  <p>Can someone correct my syntax to do this? I'm using robomongo, if that matters.</p>
  
  <p>Sample data:</p>
  
  <pre><code>{
      "_id" : ObjectId("hey"),
      "key" : {
          "keyId" : NumberLong(1234),
          "keyName" : "valueName",
      }
  }
  </code></pre>