问题
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).
回答1:
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
来源:https://stackoverflow.com/questions/65879905/how-to-replace-nan-with-null-from-mongo-shell