简体   繁体   中英

Append new column to rethinkdb table?

I am converting an excel sheet to an array of JSON objects, mapping the data to column names, and then inserting them into RethinkDB. The problem is that I then need to later on append those docs in the db with data that is being calculated elsewhere. This means adding new columns to the docs that are already stored in the database, then inserting the data into those columns. Is there any way to do this?

First you'll need a way to reference the existing documents later. By default, RethinkDB generates a random primary key for each inserted document unless you specify one explicitly.

The primary key is stored in the field id of your documents. Since it's randomly generated, it might be difficult to find the document that corresponds to a given row in your Excel sheet later, so I recommend you insert your documents with the row number as their primary key (just put an id field in the JSON objects and set it to the number value).

Then for adding a new value VAL into a new column NEWCOL in a given row i, you can use the following query:

r.table(...).get(i).update({"NEWCOL": VAL})

If NEWCOL doesn't exist in that document, this will simply add it. If it's already there, it will be overwritten by VAL.

Since RethinkDB is schemaless, you generally don't need to add new columns explicitly. If you'd still like to do that, you can run this command to initialize NEWCOL to null on all documents in the table:

r.table(...).update({"NEWCOL": null})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM