2017-01-31 149 views
0

enter image description here 我遇到的問題,我不能使聚合搜索工作。我已經能夠在沒有任何用戶的情況下在mongodb中運行它,但是在與用戶的MongoDB中(即使未啓用身份驗證),我收到了這個奇怪的錯誤消息(請參見下文)。 Whats weirder是我對其他函數沒有問題,例如find()。count()witch,如下所示返回32(數據庫中的結果數)。Aggregate not returns results,with user authentication in MongoDB

MongoDB shell中的代碼創建用戶。

use admin  

db.createUser({ user: "VibrationDataCollector", 
       pwd: '45.', 
       roles: [ "readWriteAnyDatabase", 
         "dbAdminAnyDatabase", 
           "clusterAdmin" ] }) 

代碼在python以進行搜索

client = MongoClient('mongodb://xx.x.x.xx:xxxxx/') 
db = client['VibrationDataDB'] 
db.authenticate('VibrationDataCollector', '45.', source='admin') 
coll = db.VibrationData 

hello = coll.find().count() 
print 'count=', hello 


LastWrite_Time = coll.aggregate([{ 
    "$unwind": "$Records" 
}, { 
    "$redact": { 
     "$cond": [{ 
       "$anyElementTrue": { 
        "$map": { 
         "input": "$Records.Properties", 
         "as": "result", 
         "in": { 
          "$and": [{ 
           "$eq": ["$$result.Property.Name", "LastWrite_User"] 
          }, { 
           "$eq": ["$$result.value", "42"] 
          }] 
         } 
        } 
       } 
      }, 
      "$$KEEP", 
      "$$PRUNE" 
     ] 
    } 
}, { 
    "$unwind": "$Records.Properties" 
}, { 
    "$match": { 
     "Records.Properties.Property.Name": 'LastWrite_Time' 
    } 
}, { 
    "$project": { 
     "_id": 0, 
     "value": "$Records.Properties.value" 
    } 
}]) 



list1 = LastWrite_Time['result'] 
for T in list1: 
     print T['value'] 

結果

count= 32 

Traceback (most recent call last): 
    File "C:/Python_scripts/SimulationCEI.py", line 64, in <module> 
    list1 = LastWrite_Time['result'] 
TypeError: 'CommandCursor' object has no attribute '__getitem__' 

UPDATEEEEE !!!!

使用next()

count= 32 

Traceback (most recent call last): 
    File "C:/Python_scripts/SimulationCEI.py", line 64, in <module> 
    list1 = LastWrite_Time['result'] 
TypeError: 'CommandCursor' object has no attribute '__getitem__' 
>>> next() 

Traceback (most recent call last): 
    File "<pyshell#4>", line 1, in <module> 
    next() 
TypeError: next expected at least 1 arguments, got 0 
>>> 
+0

'aggregate()'返回['CommandCursor'](http://api.mongodb.com/python/current/api/pymongo/command_cursor.html#pymongo.command_cursor.CommandCursor)不是字典。使用'next()'提前通過遊標來獲取數據。 – franklinsijo

+0

@franklinsijo 我使用 對於x在列表1: 打印X [「值」] 但仍得到相同的結果 –

+0

我沒有看到,在代碼或錯誤! – franklinsijo

回答

0

你想:

for T in LastWrite_Time: 
    print T['value'] 

「聚集」 返回必須迭代的光標。我確定認證與您看到的錯誤無關。

+0

這是我原來的,它沒有工作。然後我在列表1所作的循環與 爲T: list1.next() 印花T [「值」] ,並開始工作,然後我刪除了list1.next()和它仍在工作。我超級困惑。 –