Ich versuche also, ein Attribut in einer DynamoDB-Zeile mit Ruby zu aktualisieren. Was ich tun muss, ist eine "Abfrage" für eine Zeile. Dann muss ich ein Attribut in dieser Zeile aktualisieren. Ich habe ziemlich viel durch die Dokumentation gepflückt, aber ich habe kein Glück.
Hat jemand eine gute Möglichkeit, dies zu tun? Vielen Dank im Voraus für jede Hilfe!
Lösung des Problems
Sie können eine Tabelle folgendermaßen abfragen: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Query.html
# Queries for movies that were released in the specified year.
#
# @param year [Integer] The year to query.
# @return [Array] The list of movies that were released in the specified year.
def query_movies(year)
response = @table.query(
key_condition_expression: "#yr =:year",
expression_attribute_names: {"#yr" => "year"},
expression_attribute_values: {":year" => year})
rescue Aws::Errors::ServiceError => e
puts("Couldn't query for movies released in #{year}. Here's why:")
puts("\t#{e.code}: #{e.message}")
raise
else
response.items
end
Und um ein Element zu aktualisieren:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/example_dynamodb_UpdateItem_section.html
# Updates rating and plot data for a movie in the table.
#
# @param title [String] The title of the movie to update.
# @param year [Int] The release year of the movie to update.
# @param rating [Float] The updated rating to give the movie.
# @param plot [String] The updated plot summary to give the movie.
# @return [Hash] The fields that were updated, with their new values.
def update_movie(title:, year:, rating:, plot:)
response = @table.update_item(
key: {"year" => year, "title" => title},
update_expression: "set info.rating=:r, info.plot=:p",
expression_attribute_values: { ":r" => rating, ":p" => plot },
return_values: "UPDATED_NEW")
rescue Aws::Errors::ServiceError => e
puts("Couldn't update movie #{title} in table #{@table.name}. Here's why:")
puts("\t#{e.code}: #{e.message}")
raise
else
response.attributes
end
Keine Kommentare:
Kommentar veröffentlichen