Mongodb 字段型別轉換

2021-07-05 23:25:59 字數 685 閱讀 8818

原文傳送門

最近一些專案由於歷史原因,字段型別儲存混亂,比如乙個collection裡modifeddate列,有的存的是isodate型別,有的是numberlong時間戳,這在mongodb中是嚴重不推薦的,需要轉換成統一的。mongodb並不提供alter table這樣的語句或者工具,只能寫程式轉。

mongodb的客戶端是js寫的,其實用js寫指令碼就可以實現。

以上面的例子來說,要將isodate型別全都轉換為numberlong時間戳:

1、查詢多少記錄是isodate型別

①、$type

db.table_name.

find(}

).count()

其中的9是mongodb定義的bson date型別對應的數字。下面這張表是bson type及他們對應的數字。

②、foreach函式和instanceof

count

=0;db.table_name.

find()

.foreach(

function(x

)});

print

(count);

2、修改型別

使用foreach函式

db.table_name.

find(}

).foreach(

function(x

))

抽取sql欄位型別轉換規範

字元型varchar2 replace replace replace trim address remark chr 9 chr 13 chr 10 chr 10 chr 9 日期型date to char sell date,yyyymmddhh24miss chr 9 數值型number to...

mongodb 更新多個字段 MongoDB的使用

今天來學習乙個新的資料庫,叫做mongodb資料庫,我們先來了解一下mongodb資料庫的概念,再一起學習如何使用mongodb資料庫吧 db.help 檢視庫級別的命令db.mycoll.help 檢視collection級別的命令sh.help 檢視發片的命令rs.help 檢視副本集的命令he...

MYSQL 排序中的字段型別轉換

欄位server id是varchar型別,查詢時以server id排序,排序結果有問題?select server id from cardserver where game id 1order by server id desc limit 10 原因是 資料庫根據字串排序,10也排在9之後了...