Reputation: 1037
For example,
{
id:"monday",
names:[
{one:"white", two:"pink"},
{uno:"blanco", dos:"rosado"}
]
}
How would you change white
to black
?
So far, I've got this:
r.db.table.get('monday').update({
names: r.row("names").nth(0)
})
Which specifies the 0th index of names
, but not sure how to further specify the one
field and then change it.
Upvotes: 1
Views: 85
Reputation: 5672
First off all, you should use replace
instead of update
.
names
field.merge
it with new names
(remove 1st element from it, add at the 1st position new element in which we replaced white
to black
in one
field)Something like this:
r.db.table.get('monday').replace(function(doc){
return doc.without({names: true}). merge({
names: doc("names").deleteAt(0).prepend(doc("names").nth(0).merge({
one : "black"
}))
})
})
Upvotes: 0
Reputation: 3321
What you have so far is pretty correct but you replace names
with its first sub-object (I guess you saw that result already).
You just need to use a merge
to alter the sub-object, and a changeAt
on names
to put the altered sub-object in the array, just like this:
r.db.table.get('monday').update({
names: r.row('names').changeAt(0, r.row('names').nth(0).merge({one: 'black'}))
})
Hope this helps!
Upvotes: 1
Reputation: 11
Maybe your data is stored in an object with the name of obj
; something like
var obj = {...}
you can make this work:
obj.names[0].one = "black";
I have actually tried it using firebug no issues. Each element in the array is independent so no worries.
Upvotes: 1