Cannot redirect javascript output from console to file

70 views
Skip to first unread message

natalia.v...@gmail.com

unread,
Apr 15, 2014, 4:03:13 PM4/15/14
to mongod...@googlegroups.com
Hello,

   I have a simple javascript for formatting a mongodb collection and outputting the result.   I'd like to save the result to a file, but when I do:

mongo myscript.js > test.txt

   all I get in test.txt is the following:

MongoDB shell version: 2.4.9
connecting to: test
connecting to: localhost:27017/mongo_hadoop

  while all the "interesting" output still scrolls on the console.  The script has print and printjson lines inside of it.  

  Can someone please help?

  Thanks!

   Natalia

David Hows

unread,
Apr 15, 2014, 8:29:04 PM4/15/14
to mongod...@googlegroups.com
Hi Natalia,

I'm not 100% what you would be doing and to understand fully I would need to see your code.

On the face of things I suspect that the output is being redirected to stderr rather than stdout, can you try adding "2>&1" to the end of your command to make it:
mongo myscript.js > test.txt 2>&1

Let me know how that goes.

Regards,
David

natalia.v...@gmail.com

unread,
Apr 16, 2014, 9:32:48 AM4/16/14
to mongod...@googlegroups.com
Hi David,

   I am afraid this did not work either.  Here is my js:

var db = connect("localhost:27017/mongo_hadoop");
var compareCollections = function(){

    var cursor = db.yield_historical.out.find();
    var uniqueids = new Array();
    while (cursor.hasNext()) {
        var thearr2 = cursor.next().ids;
        thearr = thearr2.toString();
        var elements = thearr.split(",");
        for (var i = 0; i < elements.length; i++) {
            if (uniqueids.indexOf(elements[i]) == -1) {
                uniqueids.push(elements[i]);
            }

        }
    } //loop over cursor

    for (var i = 0; i < uniqueids.length; i++) {
        printjson("unique ids element " + i + " is " + uniqueids[i]);
        var thenewstr = '';
        var cursor2 = db.yield_historical.out.find( {ids : uniqueids[i] });
        while (cursor2.hasNext()) {
            var thestr = cursor2.next()._id;
            thenewstr = thenewstr + ' ' + thestr;

        }
        printjson(thenewstr);
    }

};

db.eval(compareCollections);

When I did:

mongo myscript.js > test.txt 2>&1

the output still scrolled in the mongo console window, and test.txt contained nothing but this:

MongoDB shell version: 2.4.9
connecting to: test
connecting to: localhost:27017/mongo_hadoop


Natalia

David Hows

unread,
Apr 17, 2014, 1:36:59 AM4/17/14
to mongod...@googlegroups.com
Hi Natalia,

Thanks for the output. From your code I take it that the "console" where the "interesting output" went was the MongoD log, rather than the shell?

This would be because you have executed the command with "db.eval()" which has the code evaluated server side, thus the output being from the MongoD instance.

If you simply invoke the function explicitly with compareCollections(), that should have the results output to the shell where you can capture them.

Regards,
David



Asya Kamsky

unread,
Apr 17, 2014, 2:29:24 AM4/17/14
to mongodb-user
The problem is that in interactive mode, the shell does a lot of stuff for you, like evaluating expressions without you having to ask.

When running a js file that doesn't happen.   You can see the whole bunch of info about this here, but in a nutshell:

db.foo.find() doesn't execute anything in a script, it just gets a cursor.  So you need to add .toArray() to it.
Also, just getting an object or array doesn't send it to output, you need to do print or printjson on it.

Hope that helps to get you started.  Here's another reference:
http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/#differences-between-interactive-and-scripted-mongo

Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/034fd5a1-b4d5-4aa7-a394-521d70109bfc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages