Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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_Python 3.x_Mongodb_Sorting_Pymongo - Fatal编程技术网

如何在python中查询和排序嵌套的mongodb信息?

如何在python中查询和排序嵌套的mongodb信息?,python,python-3.x,mongodb,sorting,pymongo,Python,Python 3.x,Mongodb,Sorting,Pymongo,我有以下代码: myd = mergedCollection.find(myquery).sort("Price") print("MY D: "+str(myd)) shoes = myd[0]["theAssociatedShoes"] print("Shoes: "+ str(shoes)) 对于输出: MY D: <pymongo.cursor.Cursor object at 0x05859538&

我有以下代码:

myd = mergedCollection.find(myquery).sort("Price")
print("MY D: "+str(myd))
shoes = myd[0]["theAssociatedShoes"]
print("Shoes: "+ str(shoes))
对于输出:

MY D: <pymongo.cursor.Cursor object at 0x05859538>
Shoes: [{'Title': 'Nike Cosmic Unity "Amalgam"', 'Price': 160, 'Currency': 'USD', 'Picture': 'https://static.nike.com/a/images/t_default/3bca4f51-f2e4-4948-a665-27e03eea4ddd/cosmic-unity-amalgam-basketball-shoe-nDHKr4.png', 'Link': 'nike.com/t/cosmic-unity-amalgam-basketball-shoe-nDHKr4/DA6725-500', 'Brand': 'nike'}, {'Title': 'Ultraboost 21 Shoes', 'Price': 180, 'Currency': ' USD', 'Picture': 'https://assets.adidas.com/images/w_280,h_280,f_auto,q_auto:sensitive/3728ddf5b7dc4a2ca3e3ac7d0106c5a1_9366/ultraboost-21-shoes.jpg', 'Link': 'adidas.com/us/ultraboost-21-shoes/FY0350.html', 'Brand': 'adidas'}, {'Title': 'Fresh', 'Price': 129, 'Currency': ' USD', 'Picture': 'https://nb.scene7.com/is/image/NB/m880f11_nb_02_i?$pdpflexf2$&wid=440&hei=440', 'Link': 'newbalance.com/pd/fresh-foam-880v11/M880V11-33418.html', 'Brand': 'newbalance'}, {'Title': 'Jordan Delta Breathe', 'Price': 130, 'Currency': 'USD', 'Picture': 'https://static.nike.com/a/images/t_default/b54eef6b-6dd5-4c07-9b09-901ab9d7b01a/jordan-delta-breathe-mens-shoe-2ggX3h.png', 'Link': 'nike.com/t/jordan-delta-breathe-mens-shoe-2ggX3h/CW0783-901', 'Brand': 'jordan'},...]
     ...
但这会引发语法错误。我也尝试过这个解决方案,但没有效果

myd = mergedCollection.find(myquery).sort("theAssociatedShoes.Price", -1)

查看您的代码,您感兴趣的数据都在一个记录中。MongoDB的排序只能跨多个记录工作。要按价格顺序获得结果,请使用python的
sorted
内置函数和
lambda
表达式

myd = collection.find()
for record in sorted(myd[0]["theAssociatedShoes"], key=lambda price: price['Price']):
    print(record)

也许我读错了什么,100、180、184、185看起来已经排序了?很好的catch@Joe,碰巧我使用的前四个查询都是正确的。I updatedfind()无法对嵌入的文档进行排序,必须使用聚合方法。看类似的问题这能回答你的问题吗?
myd = collection.find()
for record in sorted(myd[0]["theAssociatedShoes"], key=lambda price: price['Price']):
    print(record)