scroobius
scroobius

Reputation: 722

updating an array of nested documents rethinkdb

I have a document schema like this:

{
"name":"",
"type":"",
"posts":[
{
"url":"",
"content":"",
...
},
{
"url":"",
"content":"",
...
}
...
]
}...

I forgot to create id's for each post on insertion in database. So i'm trying to create a query for that:

r.db('test').table('crawlerNovels').filter(function (x){
  return x.keys().contains('chapters')
}).map(function (x){
  return x('chapters')
}).map(
  function(x){
   return x.merge({id:r.uuid()})
  }
)

instead this query return all posts with an id but doesn't actually update in the database. I tried using a forEach instead of a map function at the end this doesn't work

Upvotes: 0

Views: 151

Answers (1)

scroobius
scroobius

Reputation: 722

After lots of tweaking and frustration i figured it out:

r.db('test').table('crawlerNovels').filter(function (x){
  return x.keys().contains('chapters')
}).update(function(novel){
  return {"chapters":novel('chapters').map( 
    function(chapter){
    return chapter.merge({"id":r.uuid()})
  })}
},{nonAtomic:true})

Upvotes: 1

Related Questions