目前已經(jīng)找到一種方法,但是該方法存在問題,即無法有效處理文檔過長的情況。
db.area.insert({'number':'010010','province':'內(nèi)蒙古自治區(qū)','city':'呼和浩特市','disc':'新城區(qū)','addr':'光華街(十四中家屬樓)','jd':'內(nèi)蒙古自治區(qū)呼和浩特市新城區(qū)'})
db.area.insert({'number':'010010','province':'內(nèi)蒙古自治區(qū)','city':'呼和浩特市','disc':'新城區(qū)','addr':'興安南路','jd':'內(nèi)蒙古自治區(qū)呼和浩特市新城區(qū)'})
cat mon | /usr/local/mongodb/bin/mongo localhost/zip --shell
當(dāng)insert的文檔過長時,該方法會報錯,顯示unexpected token ILLEGAL和unexpected identifier。但是在Robomongo中執(zhí)行同樣的命令則沒有問題。
因為需要批量處理的是insert和update混合的命令,且涉及多個集合,因此無法使用mongoimport。
使用環(huán)境為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