英文:
Python / Rethink DB Append Nested Array
问题
我正在尝试使用Python向RethinkDB中的嵌套数组添加元素。我已经能够使用以下代码编辑"vendors"列表。但现在我想在更深的嵌套层级中添加数组。预期的结果应该在现有供应商下添加一组额外的价格数据(请参见预期结果)。
用于添加供应商的工作代码
newData = {vendor: [{"salePrice": price, "available": available, "timestamp": r.now()}]}
r.table('UPC').get(uid).update({"vendors": r.row["vendors"].append(newData)}).run(conn)
数据库架构:
"id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7",
"name": "5-Year Protection Plan - Geek Squad",
"upc": "400010179442",
"vendors": [
{
"Best Buy": [
{
"available": false,
"salePrice": 309.99,
"timestamp": "Wed Jul 12 2023 12:09:58 GMT+00:00"
}
]
},
{
"Target": [
{
"available": false,
"salePrice": 309.99,
"timestamp": "Wed Jul 12 2023 12:10:22 GMT+00:00"
}
]
}
]
预期结果的数据库架构:
"id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7",
"name": "5-Year Protection Plan - Geek Squad",
"upc": "400010179442",
"vendors": [
{
"Best Buy": [
{
"available": false,
"salePrice": 309.99,
"timestamp": "Wed Jul 12 2023 12:09:58 GMT+00:00"
}
]
},
{
"Target": [
{
"available": false,
"salePrice": 309.99,
"timestamp": "Wed Jul 12 2023 12:10:22 GMT+00:00"
},
{
"available": false,
"salePrice": 307.96,
"timestamp": "Wed Jul 13 2023 12:10:22 GMT+00:00"
}
]
}
]
以下是我尝试过的一些方法:
尝试 #1
newData = {"salePrice": price, "available": available, "timestamp": r.now()}
r.table('UPC').get(uid).update({"vendors": r.row["vendors"][vendor].append(newData)}).run(conn)
尝试 #2
newData = {"salePrice": price, "available": available, "timestamp": r.now()}
r.table('UPC').get(uid).update({"vendors": {vendor:[r.row[vendor].append(newData)]}}).run(conn)
尝试 #3
newData = {"salePrice": price, "available": available, "timestamp": r.now()}
r.table('UPC').get(uid).update({"vendors": {vendor: r.row[vendor].append(newData)}}).run(conn)
根据我尝试过的内容,通常会收到以下错误响应:
'first_error': 'No attribute `Target` in object
英文:
I am trying to use python to add elements nested array in a RethinkDB. I was able to edit the "vendors" list by using the following code. But now I want to append the array 1 nest deeper. The expected results should add an addition set of price data under an existing vendor (See Expected Results).
Working code to add vendor
newData = {vendor: [{"salePrice": price, "available": available, "timestamp": r.now()}]}
r.table('UPC').get(uid).update({"vendors": r.row["vendors"].append(newData)}).run(conn)
DB Schema:
"id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7" ,
"name": "5-Year Protection Plan - Geek Squad" ,
"upc": "400010179442" ,
"vendors": [
{
"Best Buy": [
{
"available": false ,
"salePrice": 309.99 ,
"timestamp": Wed Jul 12 2023 12:09:58 GMT+00:00
}
]
} ,
{
"Target": [
{
"available": false ,
"salePrice": 309.99 ,
"timestamp": Wed Jul 12 2023 12:10:22 GMT+00:00
}
]
}
]
Expected Results: DB Schema:
"id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7" ,
"name": "5-Year Protection Plan - Geek Squad" ,
"upc": "400010179442" ,
"vendors": [
{
"Best Buy": [
{
"available": false ,
"salePrice": 309.99 ,
"timestamp": Wed Jul 12 2023 12:09:58 GMT+00:00
}
]
} ,
{
"Target": [
{
"available": false ,
"salePrice": 309.99 ,
"timestamp": Wed Jul 12 2023 12:10:22 GMT+00:00
}
{
"available": false ,
"salePrice": 307.96 ,
"timestamp": Wed Jul 13 2023 12:10:22 GMT+00:00
}
]
}
]
Bellow is some things I have tried:
Attempt #1
newData = {"salePrice": price, "available": available, "timestamp": r.now()}
r.table('UPC').get(uid).update({"vendors": r.row["vendors"][vendor].append(newData)}).run(conn)
Attempt #2
newData = {"salePrice": price, "available": available, "timestamp": r.now()}
r.table('UPC').get(uid).update({"vendors": {vendor:[r.row[vendor].append(newData)]}}).run(conn)
Attempt #3
newData = {"salePrice": price, "available": available, "timestamp": r.now()}
r.table('UPC').get(uid).update({"vendors": {vendor: r.row[vendor].append(newData)}}).run(conn)
With what I have tried I usually get an error response of
'first_error': 'No attribute `Target` in object
答案1
得分: 0
新数据 = {"salePrice": 价格, "available": 可用, "timestamp": r.now()}
结果 = r.table('UPC').get(uid)['vendors'][vendor].run(conn)
结果[0].append(newData)
更新数据 = {vendor: 结果[0]}
ret = r.table('UPC').get(uid).update({"vendors": r.row["vendors"].merge(updateData)}).run(conn)
英文:
I feel like this answer is a bit of a work around, but it works. It gets the current values of the vendor (Best Buy or Target), modifies those values locally then uses a merge to update the old with the new.
newData = {"salePrice": price, "available": available, "timestamp": r.now()}
results = r.table('UPC').get(uid)['vendors'][vendor].run(conn)
results[0].append(newData)
updateData = {vendor: results[0]}
ret = r.table('UPC').get(uid).update({"vendors": r.row["vendors"].merge(updateData)}).run(conn)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论