我如何 delete/update 来自嵌套文档数组
How do I delete/update from nested document array
给定如下文档,我如何从嵌套文档中删除或更新 ingredients
?任何帮助,我是这个 rethinkdb 的新手。 table 存储称为食谱的文档。
[
{
"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"
},
....
我尝试了如下所示,并且成功了。但是我想知道有没有更好的办法
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
class 试图在 Python.
中对父文档的 ingredients: [..]
部分进行建模
对于更新,您可以使用map
遍历成分,找到您要更新的成分并更改其值:
r.db("test").table("sample").get("be63fc32-c1b5-4c67-a967-b6868f095216")
.update({
ingredients: r.row("ingredients").map(function(sub){
return r.branch(sub.eq("2 cups fresh parsley leaves, chopped"),
"2 cups fresh baby poo, marinated", sub)
})
})
要删除,您可以使用difference
函数:
r.db('test').table('sample').get('be63fc32-c1b5-4c67-a967-b6868f095216')
.update({ingredients: r.row('ingredients')
.difference(["4 large sprigs fresh rosemary, chopped"])});
给定如下文档,我如何从嵌套文档中删除或更新 ingredients
?任何帮助,我是这个 rethinkdb 的新手。 table 存储称为食谱的文档。
[
{
"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"
},
....
我尝试了如下所示,并且成功了。但是我想知道有没有更好的办法
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
class 试图在 Python.
ingredients: [..]
部分进行建模
对于更新,您可以使用map
遍历成分,找到您要更新的成分并更改其值:
r.db("test").table("sample").get("be63fc32-c1b5-4c67-a967-b6868f095216")
.update({
ingredients: r.row("ingredients").map(function(sub){
return r.branch(sub.eq("2 cups fresh parsley leaves, chopped"),
"2 cups fresh baby poo, marinated", sub)
})
})
要删除,您可以使用difference
函数:
r.db('test').table('sample').get('be63fc32-c1b5-4c67-a967-b6868f095216')
.update({ingredients: r.row('ingredients')
.difference(["4 large sprigs fresh rosemary, chopped"])});