Étant donné un document comme ci-dessous, comment puis-je supprimer ou mettre à jour le document imbriqué ? ingredients
? Je suis novice en matière de rethinkdb. La table stocke ces documents appelés recettes.
[
{
"cook": "9 min",
"id": "be63fc32-c1b5-4c67-a967-b6868f095216",
"inactive": "20 min",
"ingredients": [
"2 cups fresh parsley leaves, chopped",
"8 large sprigs fresh thyme, chopped",
"4 large sprigs fresh rosemary, chopped",
"3 cloves garlic, minced",
"1 small shallot, diced",
"2 tablespoons cracked black peppercorns",
"2 tablespoons cracked pink peppercorns",
"1 1/4 cups extra-virgin olive oil",
"8 skin-on chicken thighs",
"Flaky salt, such as Maldon, for seasoning",
"Salad, for serving"
],
"level": "Easy",
"prep": "5 min",
"title": "Asian Grilled Salmon",
"total": "34 min",
"yields": "6 servings"
},
....
J'ai essayé comme vous le voyez ci-dessous, et ça a marché. Mais je voudrais savoir s'il existe une meilleure méthode.
class Ingredients:
def __init__(self, name):
self.name = name
@classmethod
def update(self, recipe, name, position):
__db__ = recipe.__class__.__db__()
__table__ = recipe.__class__.__table__()
result = (
__table__.get(recipe.id)
.update(
{
"ingredients": __db__.r.row["ingredients"].change_at(
position, name
)
}
)
.run(__db__.conn)
)
return recipe.ingredients
@classmethod
def destroy(self, recipe, recipe_name):
__db__ = recipe.__class__.__db__()
__table__ = recipe.__class__.__table__()
__table__.get(recipe.id).update(
{
"ingredients": __db__.r.row["ingredients"].filter(
lambda name: name != recipe_name
)
}
).run(__db__.conn)
return recipe.ingredients
Ingredients
est une tentative de modélisation de la classe ingredients: [..]
de la partie du document parent en Python.