MongoDB & PyMongo: how to store a set / list?

戏子无情 提交于 2019-12-25 01:39:11

问题


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?


回答1:


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})}}})



来源:https://stackoverflow.com/questions/36288794/mongodb-pymongo-how-to-store-a-set-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!