2017-01-31 5 views
0

enter image description here 問題は、私は集約検索作業を行うことができません。私はユーザーがいないmongodbでmongodbを実行することができましたが、認証がオンになっていなくてもmongodbでこの奇妙なエラーメッセージが表示されます(下記参照)。私は他の関数には何の問題もありません。たとえばfind()。count()のように以下のように32(データベース内の結果の数)を返します。集計は結果を返さず、MongoDBでユーザー認証を使用します

ユーザを作成するためのMongoDBシェルのコード。検索

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を行うためのpythonで

use admin  

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

コード!!!!次の()

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()'はdictではなく[CommandCursor'](http://api.mongodb.com/python/current/api/pymongo/command_cursor.html#pymongo.command_cursor.CommandCursor)を返します。 'next()'を使ってカーソルを進め、データを取得します。私はコードまたはエラーでそれが表示されていない [「値」] プリントxを、それでも同じ結果 – franklinsijo

+0

@franklinsijo私は、リスト1にxについて を使用していました! –

+0

を得る: – franklinsijo

答えて

0

は、あなたがしたい使用して

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

"集合体" 反復しなければならないカーソルを返します。私は認証があなたが見るエラーに関連していないことを確信しています。

+0

これは私が元々持っていたものです、それは働いていませんでした。その後、私はリスト1にT用 でループを作っ: list1.next() プリントT [「値」] をし、それが作業を開始し、その後、私はlist1.next()とそのまだ作業を削除しました。私は非常に混乱しています。 –

関連する問題