从 CSV 导入 RethinkDB 时指定数据类型

Specifying data type while importing from CSV into RethinkDB

我正在使用 'rethinkdb import' 导入 CSV 文件,其中一个字段是有效的 JSON 对象。但是,似乎 RethinkDB 将该字段编码为字符串,因为我无法使用嵌套过滤器来查询数据集。

如何在导入时指定每个字段的数据类型或在导入完成后修改假定的数据类型?

来自documentation

JSON files are preferred to CSV files, as JSON can represent RethinkDB documents fully. If you’re importing from a CSV file, you should include a header row with the field names, or use the --no-header option with the --custom-header option to specify the names.

rethinkdb import -f users.csv --format csv --table test.users --no-header \
    --custom-header id,username,email,password

Values in CSV imports will always be imported as strings. If you want to convert those fields after import to the number data type, run an update query that does the conversion. An example runnable in the Data Explorer:

r.table('tablename').update(function(doc) {
    return doc.merge({
        field1: doc('field1').coerceTo('number'),
        field2: doc('field2').coerceTo('number')
    })
});