I had posted this before and had gotten some pointers but still trying to find a definitive answer.I am trying to see if there is a way to execute JS scripts or functions that allow me to call them with a parameter and return results.
function countDocs(dbName, collName) {
return db.getSiblingDB(dbName).getCollection(collName).count();
}
> countDocs("twitter","tweets")
51428
--To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/896d3e22-983f-4539-b509-c2e3b80886ef%40googlegroups.com.
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 a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/1V0920ULUQ0/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.
But to elaborate and confirm a little more, I was trying to find a way to run some 'init' scripts on some collections.While with a DBMS I can write atomic SQL stmts for this that can be executed by any client, I believe this would be the only way to do it for Mongo?Ie write an 'atomic' self-contained script that can be executed for any collection with the data and collection name passed into it, that can be tested by multiple clients, and results returned as well.
...Would this be the only way to do it? is there a better way? i don't understand why I can't use eval from PyMongo? Isn't eval for executing functions?
Hope the problem I am stating is clear - I am wanting to initialize collections with some data and then return sanity check results. I want to do this from different clients, Python, JavaScript, Mongo shell possibly.
I am new to Mongo - so thank you for your patience with my, possibly convoluted, SQL-minded, questions.
--
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 a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/1V0920ULUQ0/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.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/739be74f-98c4-4b11-9a37-aad68480df37%40googlegroups.com.
Thank you again for your details explanations. Now I have a few more clarifications I seek....1. Regards db.eval() I was of the understanding that it ran JS code as in scripts that would be local in a file system. And if it was stored on the server, it would run it there as well.
2. I understand now db.eval() will not work. I am trying to run JS scripts, and I may not have authorization on all resources.
3. Regards the getSiblingDB, I looked it up, curious why it is called getSiblingDB and not just getDB?
> db.getSiblingDBfunction ( name ){ return this.getMongo().getDB( name );}
4. Is it because I have to first get a db using a connection to a db that is on the particular Mongo instance - and then get sibling DBs on that server? ie change the connection?
5. To run a script file locally on the remote server, do I just need a connection as in: connection = pymongo.Connection("mongodb://localhost", safe=True)
6. You wanted example of a script I would run.Essentially an initialization script that would:a. create a db if it does not exist;b. create the collection;c. validate the JSON data using JsonSchema; - may be skipped
d. insert data in collection;e. run a set of sanity checks and return the result;f. calling program writes test in a file;
On thing I did not understand you wrote above was: 'to load your data fixtures via the `mongo` shell.'I keep thinking as you mentioned before as well:'If you want to invoke this script from Python, you will have to open a `mongo` shell (probably using `subprocess`) and capture any values you need from stdout.'Is this the approach you are thinking I should focus on? I do not understand why I need a shell using 'subprocess'.
As you mentioned, on mongo-hacker: https://github.com/TylerBrock/mongo-hacker/is there a particular script I could look at that would best satisfy these requirements.
--
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 a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/1V0920ULUQ0/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.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/4fc4d895-5d8d-4101-9869-352eaf8e5f13%40googlegroups.com.