如何从 Mongo shell 用 null 替换 NaN? [英] How to replace NaN with null from Mongo shell?

查看:49
本文介绍了如何从 Mongo shell 用 null 替换 NaN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过组合另外两个 pandas 数据帧,使用 pandas 制作了一个 json 文件.但是在将其上传到服务器并使用 mongoimport 将其导入到新数据库的新集合后,我发现所有应该显示为 null 的空白字符串字段现在都显示出来了作为 NaN.如何从 mongo shell 将这些 NaN 转换为 null ?请注意,我必须从服务器执行此操作,并且那里没有安装 MongoDBCompass.我必须从控制台做所有事情 (PuTTy).

解决方案

好吧,我只是一个更新操作.我使用以下 mongo shell update 命令修复了该问题,以将所有 NaN 替换为 nulls:

{multi: true} 用于替换所有匹配项.

database_name.collection_name.update({field_name_have_NaN": {$eq: NaN}}, {$set: {field_name_sharing_NaN": null}}, {multi: true});

参考:MongoDB 字段更新运算符 - $set

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 NaNs to nulls 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 NaNs with nulls:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆