title: How can I run JavaScript function stored in MongoDB document
body: <p>I'm learning MongoDB I have learned how to work will all data types provided by mongo. But I'm stuck at JavaScript. I have managed to store the JavaScript function in the document. But when I was running that function I got nothing out of it.</p>
  <p>I'm using mongo shell in the terminal.</p>
  <p><strong>Script to insert function in MongoDB.</strong></p>
  <pre><code>db.Test.insertOne({functiontest:function(){return &quot;Hello&quot;;}});
  </code></pre>
  <p><strong>After running this command</strong></p>
  <pre><code>db.Test.find();
  </code></pre>
  <p><strong>I got this output</strong></p>
  <pre><code>{ &quot;_id&quot; : ObjectId(&quot;618d4096dc79465b4196551b&quot;), &quot;functiontest&quot; : { &quot;code&quot; : &quot;function(){return \&quot;Hello\&quot;;}&quot; } }
  </code></pre>
  <p><strong>After running this command</strong></p>
  <pre><code>db.Test.find({functiontest:{$type:&quot;javascript&quot;}},{&quot;functiontest.code&quot;:1});
  </code></pre>
  <p><strong>I got this weird output</strong>
  I thought this output will give me function but I got this.</p>
  <pre><code>{ &quot;_id&quot; : ObjectId(&quot;618d4096dc79465b4196551b&quot;) }
  </code></pre>
  <p>I have also tried to run this function with pipeline but that gave me only object Id.</p>
  <pre><code>db.Test.aggregate([{$match:{functiontest:{$type:&quot;javascript&quot;}}},{$project:{&quot;codeMe&quot;:&quot;$functiontest.code()&quot;}}]);
  </code></pre>