MongoDB &PyMongo:如何存储集合/列表? [英] MongoDB & PyMongo: how to store a set / list?
问题描述
显然这是不可能的,到目前为止我发现的最好的建议是使用 dict 但这不是我想要的.还有办法在MongoDB中存储集合/列表吗?
Apparently it is not possible and the best advice I found so far is to use dict but this is not what I want. Is there still a way to store a set / list in MongoDB?
推荐答案
使用 mongo 你必须传递一个 json(在 python 中我们基本上可以认为它是一个 dict)才能真正做任何事情,所以说你想要向集合中的 hamburgers
字段添加一个列表或一组数字,您可以按如下方式准备它:
With mongo you have to pass a json (which in python we can essentially think of as a dict) in order to do anything really, so say you want to add a list or set of numbers to the hamburgers
field in your collection, you would prepare it as follows:
db.your_collection.update({'$push': {'hamburgers': {$each: [1, 2, 3, 4]}}})
如果是集合,则不能将其转换为列表
if it's a set, you cant convert it to a list
db.your_collection.update({'$push': {'hamburgers': {$each: list({1, 2, 3, 4})}}})
这篇关于MongoDB &PyMongo:如何存储集合/列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!