Python / Rethink DB 追加嵌套数组

huangapple go评论109阅读模式
英文:

Python / Rethink DB Append Nested Array

问题

我正在尝试使用Python向RethinkDB中的嵌套数组添加元素。我已经能够使用以下代码编辑"vendors"列表。但现在我想在更深的嵌套层级中添加数组。预期的结果应该在现有供应商下添加一组额外的价格数据(请参见预期结果)。

用于添加供应商的工作代码

  1. newData = {vendor: [{"salePrice": price, "available": available, "timestamp": r.now()}]}
  2. r.table('UPC').get(uid).update({"vendors": r.row["vendors"].append(newData)}).run(conn)

数据库架构:

  1. "id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7",
  2. "name": "5-Year Protection Plan - Geek Squad",
  3. "upc": "400010179442",
  4. "vendors": [
  5. {
  6. "Best Buy": [
  7. {
  8. "available": false,
  9. "salePrice": 309.99,
  10. "timestamp": "Wed Jul 12 2023 12:09:58 GMT+00:00"
  11. }
  12. ]
  13. },
  14. {
  15. "Target": [
  16. {
  17. "available": false,
  18. "salePrice": 309.99,
  19. "timestamp": "Wed Jul 12 2023 12:10:22 GMT+00:00"
  20. }
  21. ]
  22. }
  23. ]

预期结果的数据库架构:

  1. "id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7",
  2. "name": "5-Year Protection Plan - Geek Squad",
  3. "upc": "400010179442",
  4. "vendors": [
  5. {
  6. "Best Buy": [
  7. {
  8. "available": false,
  9. "salePrice": 309.99,
  10. "timestamp": "Wed Jul 12 2023 12:09:58 GMT+00:00"
  11. }
  12. ]
  13. },
  14. {
  15. "Target": [
  16. {
  17. "available": false,
  18. "salePrice": 309.99,
  19. "timestamp": "Wed Jul 12 2023 12:10:22 GMT+00:00"
  20. },
  21. {
  22. "available": false,
  23. "salePrice": 307.96,
  24. "timestamp": "Wed Jul 13 2023 12:10:22 GMT+00:00"
  25. }
  26. ]
  27. }
  28. ]

以下是我尝试过的一些方法:

尝试 #1

  1. newData = {"salePrice": price, "available": available, "timestamp": r.now()}
  2. r.table('UPC').get(uid).update({"vendors": r.row["vendors"][vendor].append(newData)}).run(conn)

尝试 #2

  1. newData = {"salePrice": price, "available": available, "timestamp": r.now()}
  2. r.table('UPC').get(uid).update({"vendors": {vendor:[r.row[vendor].append(newData)]}}).run(conn)

尝试 #3

  1. newData = {"salePrice": price, "available": available, "timestamp": r.now()}
  2. r.table('UPC').get(uid).update({"vendors": {vendor: r.row[vendor].append(newData)}}).run(conn)

根据我尝试过的内容,通常会收到以下错误响应:

  1. '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

  1. newData = {vendor: [{"salePrice": price, "available": available, "timestamp": r.now()}]}
  2. r.table('UPC').get(uid).update({"vendors": r.row["vendors"].append(newData)}).run(conn)

DB Schema:

  1. "id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7" ,
  2. "name": "5-Year Protection Plan - Geek Squad" ,
  3. "upc": "400010179442" ,
  4. "vendors": [
  5. {
  6. "Best Buy": [
  7. {
  8. "available": false ,
  9. "salePrice": 309.99 ,
  10. "timestamp": Wed Jul 12 2023 12:09:58 GMT+00:00
  11. }
  12. ]
  13. } ,
  14. {
  15. "Target": [
  16. {
  17. "available": false ,
  18. "salePrice": 309.99 ,
  19. "timestamp": Wed Jul 12 2023 12:10:22 GMT+00:00
  20. }
  21. ]
  22. }
  23. ]

Expected Results: DB Schema:

  1. "id": "049a9b69-3378-4e67-98ce-7c76c4feb2f7" ,
  2. "name": "5-Year Protection Plan - Geek Squad" ,
  3. "upc": "400010179442" ,
  4. "vendors": [
  5. {
  6. "Best Buy": [
  7. {
  8. "available": false ,
  9. "salePrice": 309.99 ,
  10. "timestamp": Wed Jul 12 2023 12:09:58 GMT+00:00
  11. }
  12. ]
  13. } ,
  14. {
  15. "Target": [
  16. {
  17. "available": false ,
  18. "salePrice": 309.99 ,
  19. "timestamp": Wed Jul 12 2023 12:10:22 GMT+00:00
  20. }
  21. {
  22. "available": false ,
  23. "salePrice": 307.96 ,
  24. "timestamp": Wed Jul 13 2023 12:10:22 GMT+00:00
  25. }
  26. ]
  27. }
  28. ]

Bellow is some things I have tried:

Attempt #1

  1. newData = {"salePrice": price, "available": available, "timestamp": r.now()}
  2. r.table('UPC').get(uid).update({"vendors": r.row["vendors"][vendor].append(newData)}).run(conn)

Attempt #2

  1. newData = {"salePrice": price, "available": available, "timestamp": r.now()}
  2. r.table('UPC').get(uid).update({"vendors": {vendor:[r.row[vendor].append(newData)]}}).run(conn)

Attempt #3

  1. newData = {"salePrice": price, "available": available, "timestamp": r.now()}
  2. 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

  1. '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.

  1. newData = {"salePrice": price, "available": available, "timestamp": r.now()}
  2. results = r.table('UPC').get(uid)['vendors'][vendor].run(conn)
  3. results[0].append(newData)
  4. updateData = {vendor: results[0]}
  5. ret = r.table('UPC').get(uid).update({"vendors": r.row["vendors"].merge(updateData)}).run(conn)

huangapple
  • 本文由 发表于 2023年7月12日 20:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定