使用MongoDB中的用户身份验证聚合不返回结果

时间:2017-01-31 17:43:17

标签: python mongodb authentication pymongo

enter image description here 我遇到的问题,我不能使聚合搜索工作。我已经能够在没有任何用户的mongodb中运行它,但是在用户的mongodb中(即使没有打开身份验证)我得到这个奇怪的错误消息(见下文)。更奇怪的是我对其他函数没有任何问题,例如find()。count()女巫,如下所示,返回32(数据库中的结果数)。

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
>>> 

1 个答案:

答案 0 :(得分:0)

你想:

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

&#34;聚合&#34;返回必须迭代的游标。我确定身份验证与您看到的错误无关。