本文介绍了在 mongodb 文档的列表中插入项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的文档

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

我想通过存储在python列表中的这些子项更新/插入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'
       },
       { ........................
       }

    ]

我试过了

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

它给了我一个错误.

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'

推荐答案

我找到了答案,在我这边很愚蠢;只需要删除额外的大括号 {}

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

代替这个

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

使用这个

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

即在 '$each' 及其对应的 '}'

这篇关于在 mongodb 文档的列表中插入项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:54
查看更多