質問

Since this is for a homework assignment I do not want to post my code because it essentially gives a solution. I can post some generic snippets. I am going to start off by saying I am new to javascript and Mongo, and basically learned them in a few hours last night.

Basically I have code that when I paste into the shell, it works perfectly, but when I save it into the database and try to execute it does not work. Here is a basic example.

db.system.js.save(
{
   _id: "istrue",
   value: function (x){ 
   if(x == true)
      print("true");
   else
      print("false");
   }
})

So if I copy and paste this code and set var x = true or var x = false first then it works, but if I do this:

db.eval("istrue(true);");

Then it doesn't work.

Any ideas?

役に立ちましたか?

解決

I can't find any documentation on this behavior, but a little testing reveals that your code is running just fine, but either stored functions can't use print or the output of print goes somewhere other than stdout. If you return strings instead of printing them, you'll see what you would expect.

I suspect the output is going to the MongoDB logs, but I'm not sure where my install put them.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top