我遇到的問題,我不能使聚合搜索工作。我已經能夠在沒有任何用戶的情況下在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
>>>
'aggregate()'返回['CommandCursor'](http://api.mongodb.com/python/current/api/pymongo/command_cursor.html#pymongo.command_cursor.CommandCursor)不是字典。使用'next()'提前通過遊標來獲取數據。 – franklinsijo
@franklinsijo 我使用 對於x在列表1: 打印X [「值」] 但仍得到相同的結果 –
我沒有看到,在代碼或錯誤! – franklinsijo