草庐IT

mongodb - pymongo - TypeError : document must be an instance of dict, bson.son.SON,或其他继承自 collections.MutableMapping 的类型

coder 2023-11-03 原文

我正在使用 pymongo 将数据写入 MongoDB

我在执行写操作时收到此错误。

TypeError: document must be an instance of dict, bson.son.SON, or other type  that inherits from collections.MutableMapping

如果我打印数据,并使用 insert_one() 调用复制它,则信息是用 shell 写入的。我已经尝试将变量转换为 str,但我不确定它是如何格式化不正确的,就像我复制打印行并执行操作一样,它被添加了。这导致我怀疑 JSON 对象的格式没有问题,但它是编码或其某些变体,或者返回的 JSON 对象的小语法。

post_id = post_db.insert_one(chunk).inserted_id

最佳答案

自从我将元组传递给 insert_one() 函数以来,我遇到了同样的错误。仔细检查您传递给此函数的数据类型,因为错误明确指出有一些数据类型被接受传递给此函数。

type(chunk) 会对你有所帮助。

关于mongodb - pymongo - TypeError : document must be an instance of dict, bson.son.SON,或其他继承自 collections.MutableMapping 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766880/

有关mongodb - pymongo - TypeError : document must be an instance of dict, bson.son.SON,或其他继承自 collections.MutableMapping 的类型的更多相关文章

  1. java - Ruby 相当于 Java 的 Collections.unmodifiableList 和 Collections.unmodifiableMap - 2

    Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur

  2. ruby-on-rails - rails 上的 ruby : radio buttons for collection select - 2

    我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/

  3. ruby - TypeError(救援条款所需的类或模块) - 2

    我已经使用Stripe一年多了,基于RyanBates的RailsCast插曲发现here.但是,我的错误处理最近停止工作,而且我以前从未见过此错误。我最近开始在Ruby2.1上运行我的应用程序,据我所知,这就是问题所在。这是我的订阅模型中的一个实例方法:beginsave_with_stripe_paymentrescueStripe::InvalidRequestError=>elogger.error"Stripeerrorwhilecreatingcustomer:#{e.message}"logger.errore.backtrace.join("\n")errors.add

  4. ruby - json 没有将 String 隐式转换为 Integer (TypeError) - 2

    玩转ruby​​,我已经:#!/usr/bin/ruby-w#WorldweatheronlineAPIurlformat:http://api.worldweatheronline.com/free/v1/weather.ashx?q={location}&format=json&num_of_days=1&date=today&key={api_key}require'net/http'require'json'@api_key='xxx'@location='city'@url="http://api.worldweatheronline.com/free/v1/weather.

  5. ruby-on-rails - rails : render a collection of models using an specific html view - 2

    我有以下关于rails的简单问题。假设我有一个模型用户。在View中,如果我这样做:views/user/_user.html.erb中的文件View将为每个用户调用和打印。如何更改它以使用特定View?我需要这样的东西:User.all:template=>"user/_user_2ndview.html"%>有什么帮助吗?提前致谢 最佳答案 您可以使用collection选项:User.all,:partial=>"users/user2ndview",:as=>:user%>View必须放在views/users/_user2

  6. ruby-on-rails - rails "undefined method for ActiveRecord_Associations_CollectionProxy" - 2

    我有这个模型:classStudent如果我在我的movies_controller.rb中写这个:@students=@movie.cinema.companies.students.all我有这个错误:#Company::ActiveRecord_Associations_CollectionProxy:0x00000007f13d88的未定义方法“学生”>如果我这样写:@students=@movie.cinema.companies.find(6).students.all它向我显示了我的select_collection中的正确学生。如何更好地理解这个过程?更新:我需要这部电

  7. ruby-on-rails - ruby rails : How to sort a collection_select - 2

    我想按数据库表列“plays”对其进行排序/排序(按我想要的方式降序或升序)我完全糊涂了。刚刚找到了select而不是collection_select的解决方案?我的一些代码不知道如何排序/排序数据库表中还有一些列,如“plays”、“goals”... 最佳答案 只需将实际排序的集合传递给collection_select助手:collection_select(:post,:author_id,Author.order('created_atDESC'),:id,:name_with_initial,:prompt=>true

  8. ruby - 无法在 Ruby 中将 StringIO 转换为 String (TypeError) - 2

    当我使用下面的代码时,我收到以下错误消息:无法将StringIO转换为String(TypeError)array_of_lines=[]Zip::ZipInputStream::open(open("URLforzippedfile","rb"))do|io|file=io.get_next_entryputs"Downloadingfile#{file}"array_of_lines=io.readlinesprint"Downloaded",array_of_lines.count,"elements.","\n"end有人可以帮助我吗?提前致谢。 最

  9. ruby-on-rails - 如何使用 grouped_collection_select 显示多选? - 2

    我正在使用以下代码来显示类别的TreeView选择框:grouped_collection_select(:categories,:category_id,Category.top_level,:children,:name,:id,:name,:include_blank=>true)如何更改它以允许多选?此外,是否可以让它显示复选框而不是选择框? 最佳答案 尝试grouped_collection_select(:categories,:category_id,Category.top_level,:children,:name

  10. ruby-on-rails - Rails 3.2 ActiveAdmin 'Collection is not a paginated scope.' 错误 - 2

    我正在使用Rails3.2和ActiveAdmin0.4.4开发应用程序。我有一个名为Teaser的模型(/app/models/teaser.rb):classTeasertruemount_uploader:img,TeaserUploaderend然后我向其中添加了ActiveAdmin(/app/admin/teaser.rb):#encoding:UTF-8ActiveAdmin.registerTeaserdoformdo|f|f.inputs"Teaser"dof.input:name,:label=>'Текст'f.input:url,:label=>'Ссылка'

随机推荐