2017-05-04 36 views
0

我有一個簡單的集合。

> db.y.find({}, {'_id': 1}) 
{ "_id" : ObjectId("5908e63cd15fa104356eaf64") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf65") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf66") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf67") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf68") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf69") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf6a") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf6b") } 

我想經營一個簡單聚合管道(大大簡化了解釋

我運行這個蒙戈shell腳本:

print('find') 
result = db.y.find({ '_id': ObjectId("5908e63cd15fa104356eaf64") }, {'_id':1}) 
while (result.hasNext()) { printjson(result.next()); } 

print('aggregate match direct') 
result = db.y.aggregate([ {'$match': {'_id': ObjectId("5908e63cd15fa104356eaf64") } }, {'$project': {'_id': 1}} ]) 
while (result.hasNext()) { printjson(result.next()); } 

print('aggregate match with $eq') 
result = db.y.aggregate([ {'$match': {'_id': {'$eq': ObjectId("5908e63cd15fa104356eaf64") } } }, {'$project': {'_id': 1}} ]) 
while (result.hasNext()) { printjson(result.next()); } 

print('aggregate match with $ne') 
result = db.y.aggregate([ {'$match': {'_id': {'$ne': ObjectId("5908e63cd15fa104356eaf64") } } }, {'$project': {'_id': 1}}, {'$limit': 5} ]) 
while (result.hasNext()) { printjson(result.next()); } 

這個結果(這是絕對正確)

find 
{ "_id" : ObjectId("5908e63cd15fa104356eaf64") } 
aggregate match direct 
{ "_id" : ObjectId("5908e63cd15fa104356eaf64") } 
aggregate match with $eq 
{ "_id" : ObjectId("5908e63cd15fa104356eaf64") } 
aggregate match with $ne 
{ "_id" : ObjectId("5908e63cd15fa104356eaf65") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf66") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf67") } 
{ "_id" : ObjectId("5908e63cd15fa104356eaf68") } 

我想將其轉換爲蟒蛇如下:

... 
print('find') 

result = y.find({ '_id': 'ObjectId("5908e63cd15fa104356eaf64")' }, {'_id':1}) 

for i, o in enumerate(result): 
    print(i, o) 

print('aggregate match direct') 

result = y.aggregate([ {'$match': {'_id': 'ObjectId("5908e63cd15fa104356eaf64")' } }, {'$project': {'_id': 1} } ]) 

for i, o in enumerate(result): 
    print(i, o) 

print('aggregate match with $eq') 

result = y.aggregate([ {'$match': {'_id': {'$eq': 'ObjectId("5908e63cd15fa104356eaf64")' } } }, {'$project': {'_id': 1} } ]) 

for i, o in enumerate(result): 
    print(i, o) 

print('aggregate match with $ne') 

result = y.aggregate([ {'$match': {'_id': {'$ne': 'ObjectId("5908e63cd15fa104356eaf64")' } } }, {'$project': {'_id': 1} }, {'$limit': 5} ]) 

for i, o in enumerate(result): 
    print(i, o) 

這個結果:

find 
aggregate match direct 
aggregate match with $eq 
aggregate match with $ne 
0 {'_id': ObjectId('5908e63cd15fa104356eaf64')} 
1 {'_id': ObjectId('5908e63cd15fa104356eaf65')} 
2 {'_id': ObjectId('5908e63cd15fa104356eaf66')} 
3 {'_id': ObjectId('5908e63cd15fa104356eaf67')} 
4 {'_id': ObjectId('5908e63cd15fa104356eaf68')} 

結論:

的$匹配操作從未採取的ObjectId語法考慮。

如何正確寫入?

感謝您的任何提示

基督教

+0

您正在使用PyMongo正確的嗎? 'Y'是一個遊標對象 – jwillis0720

回答

0

你要使用的ObjectId類標準庫

from bson.objectid import ObjectId 
result = y.find({ '_id': ObjectId("5908e63cd15fa104356eaf64") }, {'_id':1}) 

Possibly Related

眼下的比賽,並找到字段問_id是一個名爲「ObjectId(」5908e63cd15fa104356eaf64)「的字符串,但是在Python中,您必須首先分配一個唯一的標識符

你的最後一場比賽是實際打印一切,因爲你是問,如果所有_ids不等於字符串「的ObjectId(」 5908e63cd15fa104356eaf64)」,而不是的ObjectId

+0

感謝您的解決方案。它很清楚,它的工作! –

+0

但現在我有一個稍微不同的用例: –

+0

如果它對你有幫助,你會接受答案嗎? – jwillis0720