ITPub博客

首页 > 数据库 > 数据库开发技术 > 【巨杉数据库Sequoiadb】如何互相转换DateTime和TimeStamp类型

【巨杉数据库Sequoiadb】如何互相转换DateTime和TimeStamp类型

原创 数据库开发技术 作者:CherylRAnthony 时间:2019-07-29 13:00:34 0 删除 编辑

记录中分别包含 DateTime 和 TimeStamp 两种数据 类 型, 这 两种 类 型如何互相 转换 ?  

【解决 办 法】
【方法 1 】批量 转换 建 议 使用 导 入 导 出工具,操作方法如下:
1.
使用 sdbexprt 导出工具指定匹配条件,导出指定字段类型的记录
  
如 导 出 a 字段 类 型 为 date 的完整 记录 :
  ./sdbexprt -c foo -l bar --type csv --fields a,b,c,d --filter '{a:{$type:1,$et:9}}' --file foo.bar.csv
2.
删除集合中已导出的记录
  
如 删 除如上示例 a 字段 类 型 为 Date 的 记录
  db.foo.bar.remove({a:{$type:1,$et:9}})
3.
使用 sdbimprt 导入工具指定导入 a 字段 类 型 为 timestamp
  
如 导 入上述示例中 a 字段:
  ./sdbimprt -c foo -l bar --fields 'a timestamp' --type csv --file foo.bar.csv
【 风险 】
导出记录是需要导出所有字段,否则导出后的记录可能不是完整记录,删除导出的记录再导入后部分记录可能缺少未被导出的字段;
【建 议 】
先 备 份所有 记录 再 执 行如上 导 入 导 出操作,如使用 导 入 导 出工具 备 份恢复所有数据
1.
导出所有记录
./sdbexprt -c foo -l bar --type json --file foo.bar.allRecords.json
2.
需要恢复原始数据 时 ,使用 导 入工具恢复所有 记录
./sdbimprt -c foo -l bar --type json --file foo.bar.allRecords.json
【 资 料参考】
导出工具: http://doc.sequoiadb.com/cn/sequoiadb-cat_id-1479195621-edition_id-0
导入工具: http://doc.sequoiadb.com/cn/sequoiadb-cat_id-1479195620-edition_id-0
$type
匹配符: http://doc.sequoiadb.com/cn/SequoiaDB-cat_id-1477878020-edition_id-302

【方法 2 】 单 条 / 少量数据 Date 转换为 Timestamp ,如果更新后的 值 不同可以使用 update 更新 记录
【 约 束】
更新前后 时间 一 样 , 则 update 更新 时认为值 一 样 不需要更新,如 {$date:"2019-01-01"} 更新 为 {$timestamp:"2019-01-01-00.00.00.000000"} ,如果更新 为 {$timestamp:"2019-01-01-00.00.00.000001"} 则可以更新
此 场 景在后 续 版本会 优 化(可能 3.2 以后版本),如果 值 一 样则 可以指定参数 强 制更新。
【 update 更新】
匹配 a 字段 类 型 为 date 的所有 记录 ,遍 历 每一条 记录 , 获 取原始 记录 a 字段的 值 ,更新 a 字段 类 型 为 timestamp , a 字段的 值为 原始 值 拼接后的 timestamp 格式的 值 ,如:
var cursor = db.foo.bar.find({a:{$type:1,$et:9}}); while(records = cursor.next()){ var aVal = records.toObj().a.$date; db.foo.bar.update({$set:{$timestamp: aVal + "-00.00.00.000001"}}, {a:{$date: aVal }})}
【 资 料操作】
update
更新: http://doc.sequoiadb.com/cn/sequoiadb-cat_id-1432190847-edition_id-302
$type
匹配符: http://doc.sequoiadb.com/cn/SequoiaDB-cat_id-1477878020-edition_id-302  

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69935160/viewspace-2652040/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-06-25

  • 博文量
    71
  • 访问量
    55600