Mongodb:由不同的ObjectId查询

时间:2014-03-27 00:18:57

标签: mongodb pymongo

使用pymongo包我试图查询我的mongo数据库,以返回websiteId等于newStatus的每个不同for i in db.mycollection.find({'newStatus': 7}).distinct('websiteId'): pprint(i)

TypeError: 'ObjectId' object has no attribute '__getitem__'

不幸的是,这给了我一个websiteId 因为c = db.mycollection.find_one() pprint(c) {u'date': datetime.datetime(2011, 2, 22, 20, 31, 58, 316000), u'newStatus': 7, u'oldStatus': 6, u'websiteId': ObjectId('4f94c64578a62')} 似乎是某种ObjectId事物:

{{1}}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试这是否有效 -

connection = pymongo.Connection('mongodb://localhost',safe=True)
db = connection.test
sample = db.sample
query = '\'webstatId\',{"siteId" : "27"}'

try:
    print query
    doc = sample.distinct(query);
except:
    print "Unexpected error", sys.exec_info()[0]
for d in doc:
    print d

connection = pymongo.Connection('mongodb://localhost',safe=True) db = connection.test sample = db.sample query = '\'webstatId\',{"siteId" : "27"}' try: print query doc = sample.distinct(query); except: print "Unexpected error", sys.exec_info()[0] for d in doc: print d 我在shell中试过这个 -

db.sample.find()
{ "_id" : ObjectId("5333764486bee74e9de524ff"), "webstatId" : ObjectId("5333764486bee74e9de524fe"), "siteId" : "27" }
{ "_id" : ObjectId("5333767a86bee74e9de52500"), "webstatId" : ObjectId("5333764486bee74e9de524fe"), "siteId" : "28" }

db.sample.distinct('webstatId',{"siteId" : "27"})
[ ObjectId("5333764486bee74e9de524fe") ]