0

Can we directly run a Java Script from Perl to get the data from MongoDB ?

Suppose my current JS file is :--

DBQuery.shellBatchSize = 194127180 ; 
permissibleCars = 
 ["C:93124617:54413209463","C:93124633:54413165206","C:93124634:54413165224"]

db.getCollection('Test').aggregate([
{$match:
  {         "methods.name": "Download",
            "methods.status": "ACTIVE",       
            container: {"$in": permissibleCars},
            entitlementClass : "Download"

    } },
{"$group" : {_id:"$container", count:{$sum:9}}}
],
{ allowDiskUse: true}
);

Generally when we run it shell/bash we run it :--

mongo hostname:portnumber/db_name - user_name -p password < filename.js > output_file.txt

But I am not sure how to run it using perl.

Can someone help me with same ? I read through documentations didnt see anything to JS with perl.

Regards

3
  • 1
    Nothing prevents you from shelling out in Perl and running that command. You can also work with metacpan.org/pod/JavaScript or another one of that kind to execute your stuff, but I wouldn't recommend it. You can build everything you're doing in that JS file with the Perl implementation of Mongo's API. If the rest of your program is written in Perl, you should probably be doing that, and not some weird kind of hybrid. Commented Aug 17, 2017 at 15:21
  • so , same JS would work or do I need to change the JS fornat ? Commented Aug 17, 2017 at 15:26
  • 1
    Please re-read my comment. The answer to that is yes, it will work and no, you need to rewrite it. It depends on how you implement it. The implementation where the answer is yes is error-prone, ugly and probably harder to maintain. But when you rewrite it, you will initially need to learn how to do that first. In the long run, consistency should have higher value though. Commented Aug 17, 2017 at 15:36

2 Answers 2

1

With the Perl driver, you'd do that via the aggregate method.

It would look something like this:

use strict;
use warnings;
use MongoDB;

my $mc   = MongoDB->connect('mongodb://user:pass@host:port/dbname');
my $coll = $mc->ns("dbname.Test");

my @permissibleCars =
  ( "C:93124617:54413209463", "C:93124633:54413165206", "C:93124634:54413165224" );

my @pipeline = (
    {
        '$match' => {
            "methods.name"     => "Download",
            "methods.status"   => "ACTIVE",
            "container"        => { '$in' => \@permissibleCars },
            "entitlementClass" => "Download"

        }
    },
    { '$group' => { _id => '$container', count => { '$sum' => 9 } } }
);

my $res = $coll->aggregate( \@pipeline, { allowDiskUse => 1 } );

You then get results out of the $res iterator with the methods in MongoDB::QueryResult.

Be careful not to use double-quotes with $-prefixed fields you want to send to the database!

Added for verification

 use Data::Printer;
 while (my $row = $res->next) {print $row->{'container'}}
Sign up to request clarification or add additional context in comments.

2 Comments

I dont see any error coming but the result never gets printed.Updated the result part
Sorry it was my mistake . I was doing syntax error . Thanks it is fixed.
1

You could use Perl's system() function to run that command. But that seems like a spectacularly baroque way to do about it when there's a Perl module for accessing Mongo databases.

Comments

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.