如何将我的输出重新排列为单个对象并用逗号分隔? Mongodb/python
How to rearrange my output to be a single object and be separated by a comma ? Mongodb/python
注:我用的是flask/pymongo
我如何重新排列我的数据以将所有数据输出到一个用逗号分隔的对象中。 (例如,参见 post 的末尾)。
我有一个包含与此类似数据的集合,我需要输出所有次数,例如这里的三明治在集合中,就像这样 三明治:13 :
{
"id":"J6qWt6XIUmIGFHX5rQJA-w",
"categories":[
{
"alias":"sandwiches",
"title":"Sandwiches"
}
]
}
所以对于第一个请求:
restos.aggregate([{$unwind:"$categories"},
{$group:{_id:'$categories.title', count:{$sum:1}}},
{$project:{ type:"$_id", count: 1,_id:0}}])
我实现了这样的输出:
{ "count" : 3, "type" : "Sandwiches" }
但我想要的是类型作为键,计数作为值,如下所示:{ "Sandwiches" : 3 }
我能够“部分地使其与该命令一起工作,但这并不是我想要的格式:
.aggregate([{'$unwind': '$categories'},{'$group': {'_id': '$categories.title','count':{'$sum':
1}}},{'$project': {'type': '$_id', 'count': 1, '_id': 0}}, {'$replaceRoot': {'newRoot':
{'$arrayToObject': [[{'k': '$type', 'v': '$count'}]]}}}]))
输出是:
{
"restaurants": [
{
"Polish": 1
},
{
"Salad": 3
},
{
"Convenience Stores": 1
},
{
"British": 2
}]}
但是我想要的输出是这样的,它没有数组并且数据只包含在 1 个对象中:
{
"restaurants":{
Sandwiches: 13,
pizza: 15,
...
}
对于列表,我已经意识到我使用的是烧瓶,当我 return 我的 jsonify 对象时,我把 'restaurants': list(db.restaurants.aggregate([
但是当我删除它时出现此错误:TypeError: Object of type CommandCursor is not JSON serializable
关于如何做到这一点的任何想法?非常感谢:)
如果你能得到如下数据。
{ "count" : 13, "type" : "Sandwiches" }
你可以这样做:
data = [{ "count" : 13, "type" : "Sandwiches" }, { "count" : 15, "type" : "Pizza" }]
output = {}
p = {}
for d in data: # read each item in the list
p.update({d['type']: d['count']}) # build a p dict with type key
output.update({'restaurants': p}) # build an output dict with restaurants key
print(output)
# {'restaurants': {'Sandwiches': 13, 'Pizza': 15}}
注:我用的是flask/pymongo
我如何重新排列我的数据以将所有数据输出到一个用逗号分隔的对象中。 (例如,参见 post 的末尾)。
我有一个包含与此类似数据的集合,我需要输出所有次数,例如这里的三明治在集合中,就像这样 三明治:13 :
{
"id":"J6qWt6XIUmIGFHX5rQJA-w",
"categories":[
{
"alias":"sandwiches",
"title":"Sandwiches"
}
]
}
所以对于第一个请求:
restos.aggregate([{$unwind:"$categories"},
{$group:{_id:'$categories.title', count:{$sum:1}}},
{$project:{ type:"$_id", count: 1,_id:0}}])
我实现了这样的输出:
{ "count" : 3, "type" : "Sandwiches" }
但我想要的是类型作为键,计数作为值,如下所示:{ "Sandwiches" : 3 }
我能够“部分地使其与该命令一起工作,但这并不是我想要的格式:
.aggregate([{'$unwind': '$categories'},{'$group': {'_id': '$categories.title','count':{'$sum':
1}}},{'$project': {'type': '$_id', 'count': 1, '_id': 0}}, {'$replaceRoot': {'newRoot':
{'$arrayToObject': [[{'k': '$type', 'v': '$count'}]]}}}]))
输出是:
{
"restaurants": [
{
"Polish": 1
},
{
"Salad": 3
},
{
"Convenience Stores": 1
},
{
"British": 2
}]}
但是我想要的输出是这样的,它没有数组并且数据只包含在 1 个对象中:
{
"restaurants":{
Sandwiches: 13,
pizza: 15,
...
}
对于列表,我已经意识到我使用的是烧瓶,当我 return 我的 jsonify 对象时,我把 'restaurants': list(db.restaurants.aggregate([
但是当我删除它时出现此错误:TypeError: Object of type CommandCursor is not JSON serializable
关于如何做到这一点的任何想法?非常感谢:)
如果你能得到如下数据。
{ "count" : 13, "type" : "Sandwiches" }
你可以这样做:
data = [{ "count" : 13, "type" : "Sandwiches" }, { "count" : 15, "type" : "Pizza" }]
output = {}
p = {}
for d in data: # read each item in the list
p.update({d['type']: d['count']}) # build a p dict with type key
output.update({'restaurants': p}) # build an output dict with restaurants key
print(output)
# {'restaurants': {'Sandwiches': 13, 'Pizza': 15}}