1. 导入json文件
mongoimport --host IP --port 端口 -d database -c collection --file jsonFilePath --jsonArray
json文件内容事例:
[{"fieldName1":"0010000","fieldName2":1,"fieldName3":true,"fieldName4":1.45},...]
mongoimport --host IP --port 端口 -d database -c collection --file csvFilePath --headline --type csv
--headline :csv文件有字段名表头,需要,例如:
fieldName1,fieldName2,fieldName3
001000,2,字段名
...
csv导入并不能判断字段类型,如果fieldName1为字符串类型,导入后数据为“1000”,且类型为数字,并不会转为字符串。
个人通常会改为a001000,导入后再使用mongo命令批量修改,去掉字母:
db.collection.find({过滤条件e.g."fieldName":2}).forEach(
function(item){
item.fieldName1 = item.fieldName1.substr(1);
item.fieldName2 = item.fieldName2.substr(1);
db.collection.save(item);
})