0

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?

2
  • Are you getting any errors? If so, what are they? Commented Feb 8, 2013 at 19:23
  • @icanc: please read the homework tag wiki. Don't ever add it to new questions please. Commented Feb 9, 2013 at 7:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ok, this sucks because in my actual code I have a bunch of print statements. I guess I have declare a string and append it a bunch of times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.