Soubhik Banerjee
Soubhik Banerjee

Reputation: 441

Converting mongodb queries to pymongo

the below query works fine in mongodb:

db.article.find({$and:[{"version":1},{"targetGroup" : ["ecpa"]},{"state":"published"}]}).limit(5).pretty()

But when i am running it from python it throws error as invalid syntax:

from pymongo import MongoClient
import pprint

client = MongoClient('127.0.0.1', 27300)
db = client['data']
article= db.article

articles = article.find({$and:[{"version":1},{"targetGroup" : ["ecpa"]},{"state":"published"}]})
for item in articles:
    pprint.pprint(item)

what do i need to change to make this work?

Upvotes: 0

Views: 827

Answers (1)

Hagai
Hagai

Reputation: 703

in pymongo the $and operator should be in a string, so articles = article.find({"$and":[{"version":1},{"targetGroup" : ["ecpa"]},{"state":"published"}]}) should work.

Any way, next time you should post the whole error you get, so that it would be easier to answer

Upvotes: 2

Related Questions