How to execute group of commands in mongoDB Commpass or Shell

Hi Team.

I am doing some performance checks, as part of that, I have executed the below query in the Studio 3T tool.

after some time tool is closed. I have tried multiple times but the result is the same.

var before = new Date()
for ( i=1 ; i<=3000 ; i++)
{

db.JSON_TEST.find({Key_Value:i+"BC"}).forEach( function(myDoc) { print(myDoc.Key_Value,myDoc.JsonValue ); } );
}
var after = new Date()
execution_mills = after - before

JSON_TEST collection having 1 Lakh documents and the total collection size is 36.5 GB.

can I try this from MongoDB Compass or Shell? Are they any memory restrictions?

Please help me with this.