【发布时间】:2018-05-06 19:57:37
【问题描述】:
我有这样的数据:
_id:5a0f6c61d1154f2f9983ad74
name:"colla"
weight:"2"
status:"+"
time_exp:2017-11-18 00:37:31.946
_id:5a0f6c61d1154f2f9983ad74
name:"pepsi"
weight:"3"
status:"+"
time_exp:2017-11-18 00:37:31.946
_id:5a0f6c61d1154f2f9983ad74
name:"colla"
weight:"3"
status:"+"
time_exp:2017-11-18 00:37:31.946
从中我想找到所有带有"name": "colla", 'status': '+' 的非重复元素并等同于随机变量。
示例:从上面的数据中获取所有“权重”(2,3)并相等。 (some_random1 = 2, some_random2 = 3)
我是如何尝试的:list(db.some_db.find({}, {"name": "cola","status":'+'})).sort(key=itemgetter('weight'), reverse=True),但我收到了这个错误:
KeyError: 'weight'
感谢您的帮助
【问题讨论】:
-
我认为您使用 find 的方式错误。
db.some_db.find({"name": "cola","status":'+'})应该会给你正确的结果。 -
我在尝试。然后得到“无”。
-
你会得到什么:
print db.some_db.find({}).count(),print db.some_db.find({"name": "cola"}).count()&print db.some_db.find({"status":'+'}).count()我认为你的查询参数之一不起作用。 -
输出:5、5、5
标签: python mongodb sorting pymongo