Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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_Aggregation Framework_Bottle - Fatal编程技术网

Python 在瓶子框架页面上显示MongoDB聚合故障

Python 在瓶子框架页面上显示MongoDB聚合故障,python,mongodb,aggregation-framework,bottle,Python,Mongodb,Aggregation Framework,Bottle,我试图在网页上显示我的MongoDB聚合,但我遇到了一个难以理解的错误。相对新的瓶子。任何帮助都将不胜感激 我的模式 db.complaints.findOne() { "_id" : ObjectId("55e5990d991312e2c9b266e3"), "complaintID" : 1388734, "product" : "mortgage", "subProduct" : "conventional adjustable mortgage (arm)

我试图在网页上显示我的MongoDB聚合,但我遇到了一个难以理解的错误。相对新的瓶子。任何帮助都将不胜感激

我的模式

db.complaints.findOne()
{
    "_id" : ObjectId("55e5990d991312e2c9b266e3"),
    "complaintID" : 1388734,
    "product" : "mortgage",
    "subProduct" : "conventional adjustable mortgage (arm)",
    "issue" : "loan servicing, payments, escrow account",
    "subIssue" : "",
    "state" : "va",
    "ZIP" : 22204,
    "submitted" : "web",
    "received" : "2015-05-22",
    "sent" : "2015-05-22",
    "company" : "green tree servicing, llc",
    "response" : "closed with explanation",
    "timely" : "yes",
    "disputed" : ""
} 
python代码

import bottle
import pymongo
import sys
@bottle.route('/')
def home_page():
# connect to mongoDB
connection = pymongo.MongoClient('localhost', 27017)

# attach to test database

db = connection.customers
# get handle for names collection
complaints = db.complaints

# find a single document
aggr = db.complaints.aggregate([{"$group":{"_id":"$product",  "Total_Product":{"$sum:1"}}},{"$sort":{"Total_Product":-1}},{"$project":{"_id":0, "product":"$_id", "Total_Product":1}}])


return bottle.template('cfpb', {'username':"Browse Aggregate Analytics", 'things':aggr})



bottle.debug(True)
bottle.run(host='localhost', port=8082)
HTML标记

<!DOCTYPE hml>
<html>
<head>
<title>Analytics </title>
</head>
<body>
<p>
 {{username}}
<p>
<ul>
%for thing in things:
<li>{{thing[aggr]}}</li>
%end
</ul><p>

</body>
</html>

分析

{{username}}

    %对于事物中的事物:
  • {{thing[aggr]}
  • %结束
我犯了一个错误

> Error: 500 Internal Server Error
>     InvalidDocument("cannot convert value of type <type 'set'> to bson",)
>     Traceback (most recent call last):
>     File "C:\Python27\lib\site-packages\bottle.py", line 862, in _handle
>     return route.call(**args)
>     File "C:\Python27\lib\site-packages\bottle.py", line 1732, in wrapper
>     rv = callback(*a, **ka)
>     File "msc.py", line 16, in home_page
>     aggr = db.complaints.aggregate([{"$group":{"_id":"$product", "Total_Product":{"$sum:1"}}},{"$sort":{"Total_Product":-1}},{"$project":{"_id":0,
> "product":"$_id", "Total_Product":1}}])
>     File "C:\Python27\lib\site-packages\pymongo\collection.py", line 1421, in aggregate
>     result = self._command(sock_info, cmd, slave_ok)
>     File "C:\Python27\lib\site-packages\pymongo\collection.py", line 191, in _command
>     allowable_errors)
>     File "C:\Python27\lib\site-packages\pymongo\pool.py", line 189, in command
>     self._raise_connection_failure(error)
>     File "C:\Python27\lib\site-packages\pymongo\pool.py", line 316, in _raise_connection_failure
>     raise error
>     InvalidDocument: cannot convert value of type <type 'set'> to bson
>错误:500内部服务器错误
>InvalidDocument(“无法将类型的值转换为bson”,)
>回溯(最近一次呼叫最后一次):
>文件“C:\Python27\lib\site packages\battle.py”,第862行,在_handle中
>返回路线。呼叫(**args)
>包装器中的文件“C:\Python27\lib\site packages\瓶子.py”,第1732行
>rv=回拨(*a,**ka)
>主页第16行的文件“msc.py”
>aggr=db.complaints.aggregate([{“$group”:{“\u id”:“$product”,“Total\u product”:{“$sum:1”}},{“$sort”:{“Total\u product”:-1},{“$project”:{“\u id”:0,
>“产品”:“$\u id”,“总产品”:1}}])
>文件“C:\Python27\lib\site packages\pymongo\collection.py”,第1421行,总计
>结果=self.\u命令(sock\u info、cmd、slave\u ok)
>文件“C:\Python27\lib\site packages\pymongo\collection.py”,第191行,在\ u命令中
>允许误差(U)
>命令中第189行的文件“C:\Python27\lib\site packages\pymongo\pool.py”
>自我提升连接失败(错误)
>文件“C:\Python27\lib\site packages\pymongo\pool.py”,第316行,在连接失败时
>提出错误
>InvalidDocument:无法将类型的值转换为bson

当您只需要一个数组时,您要求它返回一个光标。从聚合命令中删除光标选项。我仍然收到一个错误@Blakes Seven。请编辑您的问题以显示应用的任何更改。为什么要设置游标或allowDiskUse之类的选项?它们仅适用于超出模板显示范围的大型结果集。对于大型文档,MongoDB建议从2.6开始使用光标。我仍然收到服务器错误@Blakes Seven您在引用中有一个关于“
$sum
的打字错误。另一个点是“游标”和“数组”,它们有不同的用途。这里只需要一个数组。