Reputation: 747
I need to iterate through a primary table and add new documents to a different table each with id:
and name:
entries that match documents in the primary table.
I have read the relevant documentation, which is shamefully sparse, and I found this SO post about iterating over object properties inside of the same table, but this doesnt apply since I want to iterate over objects over a table, not properties in an object, and apply the results to a NEW table (rather than the same table).
Can I iterate through the length of the table and create documents matching certain fields in a new table?
More specifically: Documents in my DB's primary_table
have fields id:
and name.
How do I migrate these id's and corresponding names to a table that details payment_history
using one or a combination of forEach
, toArray
, and next
?
My attempt:
r.db('client').table('basic_info').forEach(
function(id){
return r.db('client')table('payment_history').insert(
{
id: r.db('client').table('basic_info').get(id).getField('id'),
name: r.db('client').table('basic_info').get(id).getField('name')
}
);
}
);
Thanks in advance
Upvotes: 0
Views: 121
Reputation: 5289
You could write the forEach
like this:
r.db('client').table('basic_info').forEach(function(row) {
return r.db('client').table('payment_history').insert(
{id: row('id'), name: row('name')});})
But it would probably be better to write it like this:
r.db('client').table('payment_history').insert(
r.db('client').table('basic_info').pluck('id', 'name'))
Upvotes: 1