【发布时间】:2021-05-13 10:45:19
【问题描述】:
我正在尝试将此对象插入到 mongo DB 集合中。我已经尝试了很多方法,但没有得到任何结果。我想知道这里是否有人可以帮助我。主要问题是在将项目数组传递到键 items 时。
JSON 文件与此类似:
{
'code': 'iuhuilknlkn',
'description': 'nllksnd',
'currency': 'Mxn',
'items': [
{
'item': {
'_id': {
'$oid': '60065d253ef6d468ced3603f'
},
'code': '2',
'description': '22',
'currency': 'Mxn',
'MU': 'Hr',
'sellingPrice': 1,
'buyingPrice': 3,
'supplier': None,
'cid': '5fbd81b32b325e5ca15fe5c9',
'itemAmount': 1,
'itemProductPrice': 1,
'itemAmountPrice': 1
}
},
{
'item': {
'_id': {
'$oid': '6011c18883a280ae0e5b8185'
},
'code': 'prb-001',
'description': 'prueba 1 artículo',
'currency': 'Mxn',
'MU': 'Ser',
'sellingPrice': 100.59,
'buyingPrice': 12,
'supplier': None,
'cid': '5fbd81b32b325e5ca15fe5c9',
'itemAmount': 1,
'itemProductPrice': 100.59,
'itemAmountPrice': 100.59
}
}
],
'price': 101.59
}
我现在使用的代码如下:
productsM.insert({
'code':newP['code'],
'description':newP['description'],
'currency' :newP['currency'],
'items' : [([val for dic in newP['items'] for val in
dic.values()])],
'price' : newP['price'],
'cid': csHe })
我得到的错误是下一个:
key '$oid' must not start with '$'
【问题讨论】:
-
你可以尝试使用mongoimport命令行工具。 JSON 数据是 extended json 的格式,mongoimport 是正确的工具。
标签: python json mongodb insert