Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 聚合不返回结果,在MongoDB中使用用户身份验证_Python_Mongodb_Authentication_Pymongo - Fatal编程技术网

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

Python 聚合不返回结果,在MongoDB中使用用户身份验证,python,mongodb,authentication,pymongo,Python,Mongodb,Authentication,Pymongo,我遇到的问题是,我无法使聚合搜索正常工作。我已经能够在没有任何用户的mongodb中运行它,但是在有用户的mongodb中(即使没有打开身份验证),我收到了这个奇怪的错误消息(见下文)。更奇怪的是,我对其他函数没有问题,例如find().count(),如下所示,witch返回32(数据库中的结果数) MongoDB shell中用于创建用户的代码 use admin db.createUser( { user: "VibrationDataCollector",

我遇到的问题是,我无法使聚合搜索正常工作。我已经能够在没有任何用户的mongodb中运行它,但是在有用户的mongodb中(即使没有打开身份验证),我收到了这个奇怪的错误消息(见下文)。更奇怪的是,我对其他函数没有问题,例如find().count(),如下所示,witch返回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__'
count=32
回溯(最近一次呼叫最后一次):
文件“C:/Python\u scripts/SimulationCEI.py”,第64行,在
list1=上次写入时间['result']
TypeError:“CommandCursor”对象没有属性“\uuu getitem\uuu”
更新

使用next()

count=32
回溯(最近一次呼叫最后一次):
文件“C:/Python\u scripts/SimulationCEI.py”,第64行,在
list1=上次写入时间['result']
TypeError:“CommandCursor”对象没有属性“\uuu getitem\uuu”
>>>下一个()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
下一个()
TypeError:下一个应至少有1个参数,得到0
>>> 
您需要:

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

“聚合”返回必须迭代的游标。我确信身份验证与您看到的错误无关。

aggregate()
不会返回指令。使用
next()
通过光标前进以获取数据。@franklinsijo我在列表1:print x['value']中使用了x,但仍然得到相同的结果我在代码或错误中看不到这一点!对不起,我会把它放回去的。我想我是提前把它切断的,在运行代码后,在shell中使用next()时会发生什么情况,包括打印循环。这是我最初拥有的,它不起作用。然后我在list1:list1.next()打印T['value']并开始工作,然后我删除了list1.next(),它仍然工作。我非常困惑。
for T in LastWrite_Time:
    print T['value']