jay_t
jay_t

Reputation: 3793

pymongo use string to query instead of dictionary

Is there a way in pymongo to use a string to execute a query instead of a dictionary? I would like to be able to use exactly the same syntax as on MongoDB shell from python/pymongo. Is that possible?

Upvotes: 2

Views: 1064

Answers (2)

mjhm
mjhm

Reputation: 16705

The eval() function might be what you're looking for. Note that it executes the JS code on the db server -- NOT in a Mongo shell on some client. Therefore it comes with lots of warnings, and I would strongly advise against using it in a serious production situation. See the Mongo db.eval docs for details and examples.

Upvotes: 1

Vladimir
Vladimir

Reputation: 10533

MongoDB shell is full-featured javascript console/interpreter with some bindings to message with a mongodb server. In contrast PyMongo lacks embedded javascript interpreter or even javascript parser so you could not execute MongoDB shell queries as-is.

Note that mongo shell queries are not json documents as they are able to contain some functions and some object constructors such as {value: 2+2}.

Upvotes: 1

Related Questions