MongoDB怎样批量执行命令?
天蓬老师
天蓬老师 2017-04-24 09:10:43
0
2
674

目前已经找到一种方法,但是该方法存在问题,即无法有效处理文档过长的情况。

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
Ty80

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template