最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mongodb - Executing JavaScript function with mongo shell has no output - Stack Overflow

programmeradmin3浏览0评论

Here is my JavaScript function that I want to execute:

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

From my mand 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.

Here is my JavaScript function that I want to execute:

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

From my mand 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.

Share Improve this question edited Jun 3, 2018 at 9:40 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked Jun 3, 2018 at 9:25 ManishChahalManishChahal 1931 silver badge13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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 pared 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".

发布评论

评论列表(0)

  1. 暂无评论