【问题标题】:Failing to insert dict object with nested array into MongoDB (pymongo)无法将带有嵌套数组的 dict 对象插入 MongoDB (pymongo)
【发布时间】:2021-07-26 07:49:42
【问题描述】:

我正在尝试将 dict 对象插入 MongoDB。我在 python 脚本中使用此代码

result = centros.insert(clinica)

clinica 是字典对象

以下值没有问题。

{
    'General details': {
        'CCN': '1046002329',
        'Regional Authorization Code of the Center': '5711',
        'Name of Centre': '+ DENTAL OLIVA S.C.P.',
        'Autonomous Community': 'Comunitat Valenciana',
        'Province': 'Valencia/València',
        'Town/City': 'Oliva'
    },
    'Address': {
        'Type of street': 'CALLE',
        'Name of street': 'MONJAS CLARISAS',
        'Number': '1',
        'Postcode': '46780'
    },
    'Contact': {
        'Email': 'DENTALBENIMACLET@HOTMAIL.COM',
        'Telephone': '962854567'
    },
    'Type of Centre': {
        'Type of Centre': 'C251 - Clínicas Dentales'
    },
    'Classification': {
        'Authority or Organisation': 'Privados',
        'Classification': 'Privados'
    },
    'Care Offered': {
        **'Authorised Services': ['U.44 Odontología/Estomatología']**
    },
    'Administrative details': {
        'Date Authorisation Received': '21/02/2005',
        'Date of Last Authorisation': '10/09/2020',
        'Most Recent Authorisation Type': 'Autorizacion de modificacion'
    }
}

But if I try to insert this one, the insert fails :

{
    'General details': {
        'CCN': '0908000855',
        'Regional Authorization Code of the Center': 'E08034627',
        'Name of Centre': '+ SALUT DENTAL',
        'Autonomous Community': 'Cataluña',
        'Province': 'Barcelona',
        'Town/City': 'Igualada'
    },
    'Address': {
        'Type of street': 'AVENIDA',
        'Name of street': 'BALMES',
        'Number': '30',
        'Postcode': '08700'
    },
    'Contact': {
        'Email': 'SALUTDENTAL@SALUTDENTAL.CAT',
        'URL.': 'WWW.SALUTDENTAL.CAT',
        'Telephone': '938039232',
        'Fax': '938067466'
    },
    'Type of Centre': {
        'Type of Centre': 'C251 - Clínicas Dentales'
    },
    'Classification': {
        'Authority or Organisation': 'Privados',
        'Classification': 'Privados'
    },
    'Care Offered': {
        **'Authorised Services': ['U.35 Anestesia y Reanimación', 'U.44 Odontología/Estomatología', 'U.84 Depósito de medicamentos']**
    },
    'Administrative details': {
        'Date Authorisation Received': '30/09/1997',
        'Date of Last Authorisation': '29/05/2018',
        'Most Recent Authorisation Type': 'Autorizacion de modificacion'
    }
}

它们之间的区别在于数组“授权服务”中的元素数量。使用一个元素,插入就可以了。超过一个,插入失败。

我在这里堆积,找不到解释。

【问题讨论】:

  • 您遇到的错误是什么?请参阅How to ask a good question,了解有关如何组织您的问题以获得最佳帮助的更多信息。

标签: python arrays mongodb dictionary insert


【解决方案1】:

插入失败,因为您的字段 URL. 无效。字段名称不能包含句点 (.)

    'URL.': 'WWW.SALUTDENTAL.CAT',

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2018-06-22
    相关资源
    最近更新 更多