如何从 Mongo shell 用 null 替换 NaN? [英] How to replace NaN with null from Mongo shell?
问题描述
我通过组合另外两个 pandas
数据帧,使用 pandas
制作了一个 json
文件.但是在将其上传到服务器并使用 mongoimport
将其导入到新数据库的新集合后,我发现所有应该显示为 null
的空白字符串字段现在都显示出来了作为 NaN
.如何从 mongo shell 将这些 NaN
转换为 null
?请注意,我必须从服务器执行此操作,并且那里没有安装 MongoDBCompass
.我必须从控制台做所有事情 (PuTTy
).
好吧,我只是一个更新操作.我使用以下 mongo shell update
命令修复了该问题,以将所有 NaN
替换为 null
s:
{multi: true}
用于替换所有匹配项.
database_name.collection_name.update({field_name_have_NaN": {$eq: NaN}}, {$set: {field_name_sharing_NaN": null}}, {multi: true});
I have made a json
file using pandas
by combining two other pandas
dataframes. But after uploading it to server and importing it to a new database's new collection using mongoimport
, I have found that all the blank string fields which should be appeared as null
are now showing as NaN
. How can I convert these NaN
s to null
s from mongo shell? Please note that I have to do it from the server, and there is no MongoDBCompass
installed there. I have to do everything from console (PuTTy
).
Alright, I was just an update operation away. I fixed the problem with the following mongo shell update
command to replace all NaN
s with null
s:
The {multi: true}
for replacing all of the matches.
database_name.collection_name.update({"field_name_having_NaN": {$eq: NaN}}, {$set: {"field_name_having_NaN": null}}, {multi: true});
Reference: MongoDB Field Update Operator - $set
这篇关于如何从 Mongo shell 用 null 替换 NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!