Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing JavaScript function with mongo shell has no output

Here is my JavaScript function that I want to execute:

var collectionCreation = function(){
  db.myFirstCollection.find();
};
collectionCreation();

From my command prompt by pointing at bin directory I want to execute the js file containing above code. I am trying to do it as follows:

mongo localhost:27017/myFirstDatabase G:\MongoDB\createColl.js

It is not showing any output. I am expecting to get the documents in my collection. Please help. Thanks in advance.

like image 572
ManishChahal Avatar asked Jun 03 '18 09:06

ManishChahal


People also ask

How do I run a JavaScript script in mongo shell?

Load JavaScript file js file from mongo shell, using the load() function. load() function allow to execute from absolute and relative paths. We can simply load a file in mongo shell with load() function. And Finally If we have lots of scripted files, We can load() inside another javaScript file.

Is mongo shell a fully functioning JavaScript interpreter?

You should start mongoDB before starting the shell because shell automatically attempt to connect to a MongoDB server on startup. The shell is a full-featured JavaScript interpreter. It is capable of running Arbitrary JavaScript program.

Is mongo shell JavaScript?

So, since JavaScript is the language used to interact with the mongo shell, we can pass a JS file to the mongo command in a terminal. This is mongo shell scripting.

Can we write function in MongoDB?

You can create additional custom functions to read, insert, update, and delete data from your MongoDB using the Function Editor.


1 Answers

The problem is that you are expecting the same result as you see in the REPL when you type db.myFirstCollection.find(), but that's not what happens here. As stated it's a REPL, which means that the statement is evaluated to the "left" unless otherwise assigned to a variable or in this case, "inside a function".

So simply do something to actually "print" output:

var collectionCreation = function(){
  db.myFirstCollection.find().forEach(printjson)
};
collectionCreation();

What you see in the shell db.myFirstCollection.find() is actually that "left hand assignment" of the Cursor which is then evaluated and the first 20 results are iterated.

You can similarly stop that from immediately happening by doing:

var cursor = db.myFirstCollection.find()

Which does not "print" those results until you "evaluate":

cursor

Which will then print out the next 20, which is the default evaluation.

So when you are "scripting", you actually need to do something with the returned Cursor result. In this case forEach() the results and printjson on each iteration.

If you are going to "play around" with more of this, then I suggest you read Write Scripts for the mongo Shell and also Iterate a Cursor in the mongo Shell within the core documentation. These cover most of the differences you would encounter as you work with "scripting" as compared to the "interactive" form as you simply type things into the REPL which the mongo shell is.


Quick Demo

Create a file as test.js

(function() {
  db.test.remove({});
  db.test.insertMany([1,2,3].map(n => ({ n })));
})()

var listtest = function() {
  db.test.find().forEach(printjson)
}
listtest();

Then simply execute on the default "test" database namespace as:

mongo --quiet test.js

Returns:

{ "_id" : ObjectId("5b13b91a71a13254af4e278e"), "n" : 1 }
{ "_id" : ObjectId("5b13b91a71a13254af4e278f"), "n" : 2 }
{ "_id" : ObjectId("5b13b91a71a13254af4e2790"), "n" : 3 }

Note that in the same way the deleteMany() and insertMany() responses are also suppressed by being wrapped inside an inner function so their results are not "left evaluated".

like image 133
Neil Lunn Avatar answered Sep 20 '22 16:09

Neil Lunn