eval from PyMongo

843 views
Skip to first unread message

monosij...@gmail.com

unread,
Jun 29, 2014, 10:49:47 PM6/29/14
to mongod...@googlegroups.com
Hello -

Am trying out eval in PyMongo (Python 3.4) on MongoDB 2.6.3. Running on Ubuntu 14.04.

The JS script works fine when I run from command line as in: mongo test.js.

It is a very simple script (that I got online) and all it does it gets the version number of Mongo installed.

However it errors out when I run from PyMongo as in:

db = client[dbmsDatabase]
fileJS = os.path.join(pathScriptsMongo, 'base.js')
if (os.path.isfile(fileJS)): 
print ('File Exists!')
evaled = db.eval(fileJS)

The error I get is pasted below.

Any suggestions on what I am overlooking will be much appreciated.

Thank you.

Mono

...
File Exists!
Traceback (most recent call last):
  File "dbms.xec/python3/Controller.py", line 92, in <module>
    if __name__ == "__main__": main()
  File "dbms.xec/python3/Controller.py", line 88, in main
    controller.environmentInitiate(functionType); controller.environmentExecute(functionType);
  File "dbms.xec/python3/Controller.py", line 34, in environmentExecute
    if (functionType_ == K.ADMIN): validator = AdminValidator(self._ENVIRONMENT); generatedHostInstanceScripts = validator.generateHostInstanceScripts()
  File "/space/development/python/dbms.xec/python3/ADMIN/Validator.py", line 102, in generateHostInstanceScripts
    generatedHostInstanceScriptsNOSQL = self.getInstanceTypesNOSQL()
  File "/space/development/python/dbms.xec/python3/ADMIN/Validator.py", line 87, in getInstanceTypesNOSQL
    evaled = db.eval(fileJS)
  File "/usr/local/lib/python3.4/dist-packages/pymongo/database.py", line 951, in eval
    args=args)
  File "/usr/local/lib/python3.4/dist-packages/pymongo/database.py", line 437, in command
    uuid_subtype, compile_re, **kwargs)[0]
  File "/usr/local/lib/python3.4/dist-packages/pymongo/database.py", line 343, in _command
    msg, allowable_errors)
  File "/usr/local/lib/python3.4/dist-packages/pymongo/helpers.py", line 178, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command SON([('$eval', Code('/space/development/python/dbms.xec/python3/ADMIN/scriptsMongo/base.js', {})), ('args', ())]) failed: exception: SyntaxError: Invalid flags supplied to RegExp constructor 'development' near 'pace/development/python/dbms.xec/python3/' 


Asya Kamsky

unread,
Jun 30, 2014, 12:47:44 AM6/30/14
to mongodb-user
What's in this script you got on the internet?

Do you actually need to run this script?  And use eval?  All you need to get the version is run this command against admin DB:

"buildinfo" - that will have a field valled "version" in it which corresponds to the version of mongodb running.

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/c786f4f5-56a5-446d-8db8-fa7efd43d9d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

monosij...@gmail.com

unread,
Jun 30, 2014, 9:27:26 AM6/30/14
to mongod...@googlegroups.com
Hi Asya - Thank you for your reply and good to hear from you.

It is a small script I got from Tyler Brock @ MongoHacker.

I know I could use the commands you show, but I am trying to run some scripts to do this from Python and then possibly from another language or process such as Salt.
I am trying to set up some initialization scripts that are programming independent (at the cost of performance, I know).
I had discussed this with you a few months ago if you remember. But no worries if you don't - I was just relating the background. I am getting to it now.

So essentially I am trying to learn / develop upon running some scripts from Python using parameters. But first I have to be able to run some basic scripts so I know the environ works fine.

Please let me know what you think may be missing if the script works fine from the Mongo shell but not from eval inside Python3.

The pymongo is installed fine and as I said I have MongoDB 2.6.3 installed in localhost.

Thank you again.

Mono


On Monday, June 30, 2014 12:47:44 AM UTC-4, Asya Kamsky wrote:
What's in this script you got on the internet?

Do you actually need to run this script?  And use eval?  All you need to get the version is run this command against admin DB:

"buildinfo" - that will have a field valled "version" in it which corresponds to the version of mongodb running.

Asya

On Sun, Jun 29, 2014 at 10:49 PM, <monosij...@gmail.com> wrote:
Hello -

Am trying out eval in PyMongo (Python 3.4) on MongoDB 2.6.3. Running on Ubuntu 14.04.

The JS script works fine when I run from command line as in: mongo base.js.

Michael Grundy

unread,
Jun 30, 2014, 10:00:11 AM6/30/14
to mongod...@googlegroups.com
In your code you are passing the eval command the path of the file you want to run.

eval(code*args).
  • code: string representation of JavaScript code to be evaluated
  • args (optional): additional positional arguments are passed to the code being evaluated


Also, understand that the code will be running on mongod. I'm not sure this is what you really want to do. Please read the warnings listed in the red box at http://docs.mongodb.org/manual/reference/method/db.eval/

monosij...@gmail.com

unread,
Jun 30, 2014, 10:40:02 AM6/30/14
to mongod...@googlegroups.com
Hi Michael - 

Yes I was of the understanding that I could pass a file?

Essentially these are some of the logistics / semantics I am trying to work out for a small set of JS scripts I plan to run.

I understand the issues of the lock and like I mentioned - I am ok with the performance implications as I plan these scripts to initialization scripts. I also understand I may not need initialization scripts as Mongo creates DB/collection on demand.

However I still need to see if I can run some of these scripts from Python - I am hoping to be able to do the following:

1. Call eval with path to JS files.
2. Call a function in this JS file passing in parameter(s) - will this be possible?
3. Return a result from the execution of the function - such as number of items / records in a collection.
4. Since I can run the script in the shell using: mongo base.js - does it mean my default MongoDb installation has the necessary permissions to be able to run scripts from Python as well?

The examples show that I could send a JS string for eval - but could I send a file or would I have to read the file, possbly as a string-template and send the string-template or such?
For #2 above I have seen examples where the JS file I call could load another JS file with the params set there. Is that the best way?

After I am able to run some basic scripts I plan to look into more what I need to do for optimization - if I need at all such as running with no lock and such as described in red box by the link you sent:

I do understand that I could just write these directly in PyMongo but as I mentioned I have some reasons for doing it this way and trying to work out the logistics/semantics for this approach - for a few initializations.

Thank you again for your help.

Mono

Asya Kamsky

unread,
Jul 1, 2014, 10:51:06 AM7/1/14
to mongodb-user

Michael was pointing out that you pass actual Javascript to eval command.

You can pass a js file to mongo shell command.

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.

Monosij Dutta-Roy

unread,
Jul 3, 2014, 9:23:24 AM7/3/14
to mongod...@googlegroups.com
Thanks Asya and Michael. I will try that.

So I assume I will have to read the file into memory and pass it as the JS?

If the JS file has several functions could I call a specific function with parameters?

I ask as I have seen some examples of calling functions in eval - but in the examples the JS was a multi-line string created in the example.

If there are some examples of this would be very helpful.

Thank you.

Mono


You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/3XBCd1YvMRU/unsubscribe.
To unsubscribe from this group and all its topics, 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.
Reply all
Reply to author
Forward
0 new messages