为什么我的 MongoDB 容器不在我的 Docker 容器中且无法通过 Studio 3T 查看

问题描述 投票:0回答:1

我的目标是在开发代码时能够使用 Studio 3T 查看 docker 中 dbTrial 数据库中的“用户”集合。在早期的项目中,我执行了命令 docker run -p 27017:27017 --name Trial_mongo -d mongo。然后,我运行 npm install mongodb。然后我在代码中使用了 url = 'mongodb://localhost:27017 。我创建了一个“客户”集合,当前显示在 Studio 3T 中。

在我当前的项目中,我使用与上面相同的数据库 dbTrial,并成功读取和写入“users”集合。 “users”集合未显示在 Studio 3T 中的 dockers 容器中。所以我不知道我在这段代码中使用的集合或数据库实际驻留在哪里。没有正在运行的 MongoDB 本地实例。也许我当前运行的代码正在成功读取和写入本地存储的数据库 dbTrial,即使我的本地计算机上不存在活动的 mongo 会话。如何使我的“用户”集合驻留在我的 docker 容器中并可由 Studio 3T 查看?更重要的是,如何在计算机上找到我的数据库和“用户”集合?下面的代码片段说明了我如何连接到数据库:

const { MongoClient } = require("mongodb"); const url = "mongodb://localhost:27017"; let db = null; const connectToMongoDB = async () => { try { console.log("Attempting to connect to MongoDB..."); const client = await MongoClient.connect(url); console.log("Connected to MongoDB!"); db = client.db("dbTrial"); } catch (err) { console.error(`Error connecting to MongoDB: ${err}`); throw new Error(`Failed to connect to MongoDB: ${err.message}`); } }; const create = async (name, email, password) => { const collection = db.collection("users");

我运行了 ps -xa | grep mongodb 期望找到活动的 MongoDB 进程,但没有找到。当 MongoDB 通过 npm 在 Mac 上安装时,我在 google 上搜索了 MongoDB 数据库的默认位置,但无法在任何地方找到 /data/db 。我还对 bson 文件类型进行了全局搜索,即 find / -name "*.bson" 2>/dev/null 和 mongod.config,即 find -name "mongod.config" 2>/dev/null 但什么也没找到。

mongodb docker macos studio3t
1个回答
0
投票

难以置信。右键单击 Studio 3T 中的数据库并选择“全部刷新”使两个集合都可见。

© www.soinside.com 2019 - 2024. All rights reserved.