4

mongo在shell命令行直接执行js脚本

 2 years ago
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.
neoserver,ios ssh client

mongo在shell命令行直接执行js脚本

精选 原创

雍州无名 2022-09-08 08:17:00 博主文章分类:mongodb4.4 ©著作权

文章标签 mongodb 数据库 插入数据 文章分类 其它 数据库 阅读数281

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});

2.执行js脚本

mongo --nodb testmongo.js

3.也可以登陆mongodb直接执行脚本

load("testmongo.js")
mongo在shell命令行直接执行js脚本_数据库

也可以指定具体的路径

load("/opt/testmongo.js")
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK