pymongo execute

128 views
Skip to first unread message

Александр Ватулин

unread,
May 12, 2014, 3:46:24 PM5/12/14
to mongod...@googlegroups.com
Hello!
I use pymongo in my webapps and i don't understand, how to use "execute".
In MySQL i can write smth like this:

db = MySQLdb.connect(host=host_name, user=username, passwd=passwd, db=db_name, charset=charset)
cursor = db.cursor()
cursor.execute("SELECT * FROM MyTable")

But how to do it in pymongo?

Bernie Hackett

unread,
May 12, 2014, 3:53:36 PM5/12/14
to mongod...@googlegroups.com
Hi,

MongoDB does not use SQL. Please read the PyMongo tutorial here:



--
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/8f84a260-ffa0-4da6-bd11-ab1ff53bc0f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Александр Ватулин

unread,
May 12, 2014, 3:58:52 PM5/12/14
to mongod...@googlegroups.com
I don't think, that mongo use SQL.
I asked, how to execute mongo query in pymongo.
And i use SQL only for example. 

For example: i have
client = MongoClient('localhost', 27017)
db = client.mydb 
que = "db.items.find()"  # my query as a string
db.execute(que)  # i want to execute my string-query, but it give me an error.

понедельник, 12 мая 2014 г., 23:53:36 UTC+4 пользователь Bernie Hackett написал:

Bernie Hackett

unread,
May 12, 2014, 4:04:31 PM5/12/14
to mongod...@googlegroups.com
There is no execute method for queries in PyMongo. You call the methods directly. In your case:

db = client.mydb
for document in db.items.find():
   <do something with document>

or to get the documents as a list:

documents = list(db.items.find())

This is all covered in the tutorial:



Александр Ватулин

unread,
May 12, 2014, 4:13:02 PM5/12/14
to mongod...@googlegroups.com
bad-bad-bad (
So, how can i execute queries like this (client send it to me like a string)?
>> db.servers.aggregate([{'$group': {'_id': {'RAM': '$tech.RAM', 'HDD': '$tech.HDD_type'}, 'average': {'$avg': '$population.total'}}}
>> items_store.find({ 'itemSpells.spell': {'$exists': true}}).count()
>> db.servers.update({'sid': {'$gt': 250}}, {'$unset': {'tech.serial_number': 1}}, {'multi': true})
Or the only way is write my own parser and call "find", "aggregate", "update" etc when i need?

вторник, 13 мая 2014 г., 0:04:31 UTC+4 пользователь Bernie Hackett написал:

Bernie Hackett

unread,
May 12, 2014, 4:16:34 PM5/12/14
to mongod...@googlegroups.com
Can you explain what you're trying to do? Are you trying to execute these operations as javascript on the server? If so, why?


Александр Ватулин

unread,
May 12, 2014, 4:21:20 PM5/12/14
to mongod...@googlegroups.com
I write the testing system in which user can send to the server queries like:

>> db.servers.aggregate([{'$group': {'_id': {'RAM': '$tech.RAM', 'HDD': '$tech.HDD_type'}, 'average': {'$avg': '$population.total'}}}
>> items_store.find({ 'itemSpells.spell': {'$exists': true}}).count()
>> db.servers.update({'sid': {'$gt': 250}}, {'$unset': {'tech.serial_number': 1}}, {'multi': true})

At the server i need to execute every query for modify database or select from it. How can i do this?

P.S.: is my english very bad?

вторник, 13 мая 2014 г., 0:16:34 UTC+4 пользователь Bernie Hackett написал:

Bernie Hackett

unread,
May 12, 2014, 5:03:37 PM5/12/14
to mongod...@googlegroups.com
There is no way to execute these statements. "db.servers" and "items_store" don't mean anything as strings in python.

If you set variables like "db" and "items_store" to instances of pymongo.database.Database or pymongo.collection.Collection you could probably use python's built-in eval() function to do what you are asking:

>>> c = pymongo.MongoClient()
>>> db = c.foo
>>> eval("db.bar.count()")
0
>>> eval("list(db.bar.find())")
[]




Александр Ватулин

unread,
May 14, 2014, 1:01:07 PM5/14/14
to mongod...@googlegroups.com
Thanks! You helped me find a way. I'll use pymongo.database.eval with javascript inserts.

вторник, 13 мая 2014 г., 1:03:37 UTC+4 пользователь Bernie Hackett написал:
Reply all
Reply to author
Forward
0 new messages