简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM