inserting items in list in mongodb document

ε祈祈猫儿з 提交于 2019-12-11 03:39:12

问题


I have a document like this

{ "_id" : "decfed9a04b997d", "pushed_list" : [ ] }

and I want to update/insert "pushed_list" by these sub items stored in a python list

lis1 = 
    [
       {
        item : 'item1',
        desc : 'desc_item1'
       },
       {
        item : 'item2',
        desc : 'desc_item2'
       },
       { ........................
       }

    ]

I tried this

db.monitor.update({'_id' : 'dbjkdd'}, { '$push':{'pushed_list': {'$each':{lis1}}}})

Its giving me an error.

Traceback (most recent call last):
  File "monitor.py", line 13, in <module>
    db.monitor.update({'_id' : 'dbjfdd'}, { '$push':{'pushed_list': {'$each':{lis1}}}})
TypeError: unhashable type: 'list'

回答1:


I found the answer, pretty foolish on my side; just have to remove extra braces {}

Instead of this

db.monitor.update({'_id' : 'dbjkdd'}, { '$push':{'pushed_list': {'$each':{lis1}}}})

Use this

db.monitor.update({'_id' : 'dbjkdd'}, { '$push':{'pushed_list': {'$each':lis1}}})

i.e remove '{'after '$each' and its corresponding '}'



来源:https://stackoverflow.com/questions/17674352/inserting-items-in-list-in-mongodb-document

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