【问题标题】:Query tangled array in Pymongo在 Pymongo 中查询纠结的数组
【发布时间】:2019-03-20 14:43:47
【问题描述】:

我正在尝试查询一个非常纠结的集合。

架构:

{'tags': {'variables': [{'value': '3x9', 'var_name': 's'},
                     {'value': '12:00AM', 'var_name': 'x'},
                     {'value': 'goog', 'var_name': 'y'}]},
                     'url': 'https://www.google.com'}]

查询:

res = mycol.aggregate([{"$unwind":"$tags"}
     ,{'$match': {'tags.tag.name':'A A', 'aR': 98765}}
     ,{'$project': {'url': 1, 'tags.vars': 1, '_id': 0}},])


res = list(res)
for x in res: 
    print(res)

结果:

{'tags': {'variables': [{'value': '3x9', 'var_name': 's'},
                     {'value': '12:00AM', 'var_name': 'x'},
                     {'value': 'goog', 'var_name': 'y'}]},
                     'url': 'https://www.google.com'}]

预期结果:

[{'value': '3x9', 'var_name': 's'},
{'value': '12:00AM', 'var_name': 'x'},
{'value': 'goog', 'var_name': 'y'},
'url': 'https://www.google.com']

我会将它用作HttpResponse 并将其用作JSON 对象。

请帮忙!

【问题讨论】:

  • 您的架构定义错误。你能用正确的模式修复它吗?
  • @SaketKhandelwal,我的架构是从我的终端完全复制的
  • 你能从 mongodb 数据库中复制它吗?

标签: python json django mongodb pymongo


【解决方案1】:

你能说 res['tags']['variables'] 而不是 res = list(res) 吗?

【讨论】:

  • TypeError: 'CommandCursor' object is not subscriptable 这是我的错误
猜你喜欢
  • 2019-02-20
  • 2019-09-18
  • 2011-11-14
  • 2017-08-22
  • 2013-03-07
  • 2016-11-18
  • 2017-10-21
  • 2010-12-17
  • 1970-01-01
相关资源
最近更新 更多