文章 / 数据与中间件 / mongoDB的一些服务端操作1

mongoDB的一些服务端操作1

2025年4月18日3 min884 words
mongoDB的一些服务端操作1
图 1-1 cover.webp
# 连接

mongoshell

```bash
mongosh mongodb://<user>:<password>@localhost/<db>?authSource=admin

docker with mongoshell

docker exec -it mongosh mongodb://<user>:<password>@localhost/<db> --authenticationDatabase admin

运行脚本

mongosh mongodb://<user>:<password>@localhost/<db>?authSource=admin --eval("db.users.find({})")
// db.js为脚本文件
mongosh mongodb://<user>:<password>@localhost/<db>?authSource=admin db.js
// db.js为脚本文件 且 为容器内脚本路径
docker exec -it mongosh mongodb://<user>:<password>@localhost/<db> --authenticationDatabase admin /db.js
mongosh mongodb://<user>:<password>@localhost/<db>?authSource=admin
load("/db.js")

脚本文件

// db.js
use myDatabas;
// 新版insert已经被弃用,只能用inserOne 和 inserMany
db.myCollection.insertOne({})
db.myCollection.find({})

引入其他文件

// utils.js
function sum(a,b) {
	return a+b;
}
// db.js
load("utils.js")
const a = 1;
const b = 2;
sum(a+b)

评论 (0)

加载评论中...
RELATED NOTES
05 entries
01n8n 技术实践(三):自托管、扩展、安全与可观测性阅读文章 →02docker镜像资源阅读文章 →03mcp服务的node实现阅读文章 →04kafka阅读文章 →05知乎邀请指定用户阅读文章 →