nodejs使用mongodb和mongoose
来源:互联网 发布:中级数据库系统工程师 编辑:程序博客网 时间:2024/04/27 05:30
mongoose官网链接
- mongodb
- 启动mongodb
- nodejs mongoose
- mongoose modlel 定义数据类型
- mongoose需要注意的点
- mongoose基本操作
- mongoose插入操作
- mongoose更新操作
- mongoose删除
- mongoose查找
- mongoose查询
- mongodb
mongodb
启动mongodb
#以下启动mongodb为在mac系统中,windows系统换为相应的命令即可sudo mongod# 如果没有创建全局路径 PATH,需要进入以下目录cd /usr/local/mongodb/bin#注意上述路径每个人都不同,需要进入自己的mongodb/bin目录下sudo ./mongod
#进入mongo的shell环境下$ cd /usr/local/mongodb/bin $ ./mongoMongoDB shell version v3.4.2connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.2Welcome to the MongoDB shell.……> 1 + 12> >
建议下载Robo 3T,mongodb的图形化管理界面,能够更方便的管理操作数据库
nodejs mongoose
mongoose modlel 定义数据类型
- String
- Number
- Date
- Buffer
- Boolean
- Mixed
- ObjectId
- Array
mongoose需要注意的点
- mongodb只会插入modelSchema定义的点,如果创建对象其他的值是不会放入数据库的
mongoose基本操作
var mongoose=require('mongoose')mongoose.connect('mongodb://localhost/test')var db=mongoose.connectiondb.on('error',console.error.bind(console,'connection fail'))db.once('open',function(){ //数据库具体操作代码在这里写})
mongoose插入操作
//先定义modelvar Cat=mongoose.model('Cat',{ name:String, age:Number, 'hair color':String})var kitty=new Cat({ name:'Kitty', age:3, 'hair color':'white'})//对象的操作kitty.save(function(err,res){ if(err) console.error(err) else console.log(res)})
mongoose更新操作
//模型的操作(已定义完模型)var where={ variety:'Scottish Fold'}var update={ 'hair color':'black'}Cat.update(where,update,function(err,res){ if(err) console.error(err) else console.log(res)})
还有专门通过ID查找并更新的方法
var whereById=''Cat.findByIdAndUpdate(whereById,update,function(err,res)){ if(err) console.error(err) else console.log(res)})
mongoose删除
Cat.remove(where,function(err,res))Cat.findByIdAndRomove(where,function(err,res))
mongoose查找
Cat.find(where,function(err,res)) //res 返回查找到的对象数组 //可以限定输出的内容 var opt={ variety:1 //选择输出的值为1,不输出的值为0(其他不指定默认为0) } Cat.find(where,opt,function(err,res)) //var where=_id Cat.findById(where,function(err,res)) //res 输出查询到的对象
mongoose查询
Cat.count(where,function(err,res))//res输出查询数量
阅读全文
0 0
- nodejs使用mongodb和mongoose
- nodejs/express+mongodb/mongoose
- Nodejs mongoose操作mongodb
- nodejs+mongodb系列教程之(4/5)--mongoose使用
- nodejs使用mongoose 获取mongodb 数据格式化问题
- nodejs操作mongodb数据库(mongoose)
- nodejs mongoose 操作mongodb 数据库封装
- Nodejs+express+mongodb+mongoose实现个人博客
- [NodeJS] Mongoose Populate 基本使用
- nodejs和mongoose实现的对mongodb数据库进行的增删改查操作模板
- Nodejs学习笔记(十四)— Mongoose介绍和入门 && Nodejs学习笔记(十)--- 与MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
- MongoDB之Mongoose的使用
- MongoDB之Mongoose的使用
- nodejs中使用mongoose保存数据
- Mongoose – 让NodeJS更容易操作Mongodb数据库
- Mongoose - 让NodeJS更容易操作Mongodb数据库
- 关于NodeJs为什么要用mongoose操作mongodb
- nodejs-express-ejs-mongodb-mongoose 错误锦集
- Leetcode 题解
- opencv_tutorial_code学习——图片叠加
- php使用curl发送post请求
- 微信分享到朋友或朋友圈
- 手把手教你用Hexo搭建免费个人博客
- nodejs使用mongodb和mongoose
- 回退上一个页面时如何保证之前的数据还在,即回退时不刷新页面
- Splunk 存储App和Add-on整理总结
- VMWare安装macOS 12后配置虚拟机全屏显示(超实用)
- 自定义View之绘图篇(二):路径(Path)
- linux进程
- Java中的一些异常
- three.js中材质的应用
- Cloudera 配置NameNode HA