MongoDB 使用

安装

  • Homebrew:

    brew tap mongodb/brew
    brew update
    brew install mongodb-community
    

    启动 MongoDB 服务:

    brew services start mongodb-community
    

    相关路径(Apple Silicon):

    • 配置文件路径:/opt/homebrew/etc/mongod.conf
    • 日志文件路径:/opt/homebrew/var/log/mongodb
    • 数据目录:/opt/homebrew/var/mongodb
  • Docker:

    拉取镜像:

    docker pull mongodb/mongodb-community-server:latest
    

    启动容器:

    docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:latest
    

参考:安装 MongoDB | MongoDB 手册

安装 Compass:

MongoDB Compass Download (GUI) | MongoDB

Compass 是 MongoDB 提供的一款图形化用户界面(GUI)工具,用于可视化地管理和分析 MongoDB 数据库。

使用

导入一个 JSON 文件:

mongoimport --db yourDatabaseName --collection yourCollectionName --file document.json

启动 MongoDB Shell:

mongosh

选中 Database:

use yourDatabaseName

MongoDB 中 Database 和 Collection 的关系:

  • 数据库:library
    • 集合:books
      • 文档:{ "title": "1984", "author": "George Orwell" }
      • 文档:{ "title": "Brave New World", "author": "Aldous Huxley" }
    • 集合:members
      • 文档:{ "name": "John Doe", "membership_id": "12345" }

参见:MongoDB CRUD 操作 | MongoDB 手册

posted @ 2024-10-09 21:27  Undefined443  阅读(16)  评论(0)    收藏  举报