美文网首页
mongoose基础教程(一) mongo介绍及连接

mongoose基础教程(一) mongo介绍及连接

作者: coolheadedY | 来源:发表于2017-10-18 21:02 被阅读37次

mongodb 安装

brew install mongo

创建shell命令

user目录新建db.sh

#! /bin/sh
mongod --dbpath ~/data/db

GUI客户端robomongo

mongo / mongod / mongos

  • mongo 是mongoDB体系中交互式的JS shell,用来操纵数据库

  • mongod 起项目-mongod系统的驻台程序

mongod = mongo + daemon

1处理数据请求
2管理数据
3管理后台任务

  • mongos "MongoDB Shard"切片服务, 处理回调

mongo基本层级划分

|- db 
|- |- collections
|- |- |- documents 
|- |- |- |- Field 

对比mysql关系型数据库

MySQL MongoDB
Table Collection
Row Document
Column Field
Joins Embedded documents, linking

数据库连接

  • 命令行启动mongo
shell 命令
$ mongod --dbpath ~/db/data
$ mongo
const mongoose = require('mongoose')
// 连接本地
const uri = 'mongodb://localhost:27017/dbname'
// 连接集群
const uri = 'mongodb://username:password@xxx.xxx.xxx:port,xxx.xxx.xxx:port,xxx.xxx.xxx:port/dbname'

mongoose.Promise = global.Promise // 替换mongoose的Promise

mongoose.connect(uri, { useMongoClient: true })

const db = mongoose.connection

db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', function () {
    console.log('mongo connect!')
})

相关文章

网友评论

      本文标题:mongoose基础教程(一) mongo介绍及连接

      本文链接:https://www.haomeiwen.com/subject/fyusuxtx.html