4
mongo在shell命令行直接执行js脚本
source link: https://blog.51cto.com/u_13753753/5660099
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
mongo在shell命令行直接执行js脚本
精选 原创1.js的脚本如下
vim testmongo.js
//连接mongodb
var url = "mongodb://admin:[email protected]:27017/admin?authMechanism=SCRAM-SHA-1"
var con = new Mongo(url)
//切换数据库
var db = con.getDB('testdb')
//插入数据
db.test1.deleteMany({"a": 1})
db.test1.insert({"a": 1});
var url = "mongodb://admin:[email protected]:27017/admin?authMechanism=SCRAM-SHA-1"
var con = new Mongo(url)
//切换数据库
var db = con.getDB('testdb')
//插入数据
db.test1.deleteMany({"a": 1})
db.test1.insert({"a": 1});
2.执行js脚本
mongo --nodb testmongo.js
3.也可以登陆mongodb直接执行脚本
load("testmongo.js")
也可以指定具体的路径
load("/opt/testmongo.js")
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK