使用
- GUI工具:Compass
安装
现在mongodb已经不开源免费了,我们个人学习使用的话可以用社区版,通过homebrew安装 安装过程需要设置环境变量,参考这篇
启动
brew services start mongodb-community@4.4
停止启动
brew services stop mongodb-community@4.4
概念区分
mongodb是一个文档数据库
- 数据库database
- 集合 collection:层级上类似于Mysql表
- 文档 document : 层级上类似于Mysql记录,一行数据
Nodejs链接mongodb
1、安装nodejs的mongodb驱动
```javascript const { MongoClient } = require(‘mongodb’); // or as an es module: // import { MongoClient } from ‘mongodb’mongod --dbpath=/data
// Connection URL const url = ‘mongodb://localhost:27017’; const client = new MongoClient(url);
// Database Name const dbName = ‘myProject’;
async function main() { // 1、链接数据库 await client.connect(); console.log(‘Connected successfully to server’); // 2、切换到数据库 const db = client.db(dbName); // 3、使用集合 const collection = db.collection(‘documents’); // 4、查询 collection.find().toArray((err,result)=>{ … }) // the following code examples can be pasted here…
return ‘done.’; }
main() .then(console.log) .catch(console.error) .finally(() => client.close());
```javascript
node app.js