MongoDB 和 Python - 带列表的字典

ss1*_*111 1 python database dictionary mongodb pymongo

将字典插入 MongoDB 后,如何从字典中获取列表?

例如,假设我将此条目存储在集合中:

{'Store': 'Store A','Brands':['Nike','Adidas','Reebok']}

如何访问“商店 A”并仅打印品牌列表?

我只想打印列表中的值:

Nike Adidas Reebok

我只是想学习如何使用 MongoDB,似乎在他们的文档中找不到类似的例子。任何帮助,将不胜感激!

Tho*_*rot 5

假设您使用类似db.insert()方法插入,以下应该有效:

for brand in db.find_one({"Store":"Store A"})["Brands"]:
  print(brand)
Run Code Online (Sandbox Code Playgroud)

当您使用 pymongo 的find_one()方法时,您将获得一个字典对象(如果找到一个对象),您可以将其作为索引["Brand"]并拥有品牌数组。