目前已经找到一种方法,但是该方法存在问题,即无法有效处理文档过长的情况。
db.area.insert({'number':'010010','province':'内蒙古自治区','city':'呼和浩特市','disc':'新城区','addr':'光华街(十四中家属楼)','jd':'内蒙古自治区呼和浩特市新城区'})
db.area.insert({'number':'010010','province':'内蒙古自治区','city':'呼和浩特市','disc':'新城区','addr':'兴安南路','jd':'内蒙古自治区呼和浩特市新城区'})
cat mon | /usr/local/mongodb/bin/mongo localhost/zip --shell
当insert的文档过长时,该方法会报错,显示unexpected token ILLEGAL和unexpected identifier。但是在Robomongo中执行同样的命令则没有问题。
因为需要批量处理的是insert和update混合的命令,且涉及多个集合,因此无法使用mongoimport。
使用环境为linux
Directly write all the code to be executed into one
all.js
文件,然后用mongo localhost/zip all.js
execution.@Huan Du’s answer is to use the script mode of MongoDB Shell. It should be noted that this is slightly different from directly entering commands in the Shell. For example, show dbs cannot be used because it is not a legal Javascript language. For another example, the query returns Cursor instead of directly displaying the first 20 documents. There will be error messages, such as syntax errors.
See the specific documentation here
MongoDB Scripting
Write Scripts for the mongo Shell
Server-side JavaScript