发布于2021-03-02 16:02 阅读(367) 评论(0) 点赞(17) 收藏(0)
0
1
2
3
4
5
6
我正在尝试使用django和python开发mongodb聚合。
我有一个Atlas服务器,其数据库名为“ sucre”,其集合名为“ bdua”
在settings.py我有:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'morfeeweb',
'ENFORCE_SCHEMA': False,
'CLIENT': {
'host': 'mongodb+srv://xxxxxx:xxxxxx@xxxxx.xxxxx.xxxxxx.mongodb.net/morfeeweb?retryWrites=true&w=majority',
'port': 27017,
'username': 'xxxxxx',
'password': 'xxxxxxxx',
'authSource': 'admin',
'authMechanism': 'SCRAM-SHA-1'
},
'LOGGING': {
'version': 1,
'loggers': {
'djongo': {
'level': 'DEBUG',
'propagate': False,
}
},
},
},
'sucre': {
'ENGINE': 'djongo',
'NAME': 'sucre',
'ENFORCE_SCHEMA': False,
'CLIENT': {
'host': 'mongodb+srv://xxxx:xxxxxx@xxxxx.xxxxx.xxxx.mongodb.net/sucre?retryWrites=true&w=majority',
'port': 27017,
'username': 'xxxxx',
'password': 'xxxxxxxx',
'authSource': 'admin',
'authMechanism': 'SCRAM-SHA-1'
},
'LOGGING': {
'version': 1,
'loggers': {
'djongo': {
'level': 'DEBUG',
'propagate': False,
}
},
},
}
}
views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from random import randint
from django.db import connection
@login_required(login_url='/admin/login')
def consolidado(request):
aggretation_sample = connection['sucre'].db.bdua.aggregate([
{
'$match': {'estado': 'AC', 'regimen': 'subsidiado'}
},
{
'$group': { '_id': '$cod_EPS', 'Afiliados': {'$sum': 1}}
},
{
'$sort': {'Afiliados': -1}
}
])
return HttpResponse(aggretation_sample)
它返回“ / aseguramiento /处的TypeError /'DefaultConnectionProxy'对象无法下标”
请帮忙
django.db.connection
返回default
数据库(请参阅此处的文档)。您要使用:
from django.db import connections
aggretation_sample = connections['sucre']...
0
1
2
3
4
5
6
7
8
作者:黑洞官方问答小能手
链接: https://www.pythonheidong.com/blog/article/869969/f423230fb52d7ec57355/
来源: python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!