【node】mongoose的基本使用
1、安装mongoose
npm install mongoose
2、启动数据库
mongod --dbpath d:\data\db
3、引入mongoose模块并连接数据库

const mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1:27017/test1",function(err) {
if(err){
console.log('连接失败');
}else{
console.log("连接成功")
}
});


4、创建表以及字段类型
const User = mongoose.model("user",{
name:String,
age:Number
})

5、增

const user = new User({
name:"张三",
age:19
})
user.save().then((result)=>{
console.log("成功的回调")
},()=>{
console.log("失败的回调")
})


6、删

1、删除指定数据
User.remove({name:"zhao"}).then((result)=>{
console.log(result)
}) result:是一个对象 返回值是受影响条数
2、删除所有数据
User.remove({}).then((result)=>{
console.log(result)
}) //删除指定ID
3、User.findByIdAndRemove(id值).then((result)=>{ })


7、改

User.update({name:"ya"},{$set:{name:"hua"}},{multi:true}).then((result)=>{
console.log(result)
})
multi:true 表示修改多条数据
User.findByIdAndUpdate(id值,{$set:{需要修改的内容}}.then((result)=>{})


8、查
001查询符合条件的所有数据

User.find({name:ya}).then((result)=>{
console.log(result)
})
result是查到的数据


002、查询所有数据
User.find().then((result)=>{
console.log(result)
})

003、查询单条数据
User.findOne({name:"zhao"}).then((result)=>{
console.log(result);
})

004、条件查询:

$lt(小于) $lte(小于等于) $gt(大于) $gte(大于等于) $ne(不等于);
User.find({"age":{"$lt":20}}).then((result)=>{
console.log(result);
})
User.find({"age":{"$lte":20}}).then((result)=>{
console.log(result);
})
User.find({"age":{"$gt":20}}).then((result)=>{
console.log(result)
})
User.find({"age":{"$gte":20}}).then((result)=>{
console.log(result)
})
User.find({"age":{"$ne":19}}).then((result)=>{
console.log(result)
})


005、$in(包含 等于) $nin(不包含 不等于)

User.find({"age":{"$in":[18,19]}}).then((result)=>{
console.log(result)
})
User.find({"age":{"$nin":[18,19]}}).then((result)=>{
console.log(result)
})


006、$or(或)
User.find({"$or":[{name:"zhao"},{age:20}]}).then((result)=>{
console.log(result)
})

007、$exists (判断当前关键字是否存在)
User.find({name:{"$exists":true}}).then((result)=>{
console.log(result);
})

008、查询指定列 如果不想要id值 只需要设置_id:0
User.find({},{name:1,age:1,_id:0}).then((result)=>{
console.log(result);
})

009、升序降序 sort()
User.find().sort({age:1}).then((result)=>{
console.log(result)
})

010、模糊查询 //

User.find({name:/a/}).then((result)=>{
console.log(result)
})
User.find({name:/^z/}).then((result)=>{
console.log(result);
})
User.find({name:/z$/}).then((result)=>{
console.log(result);
})


011、skip(n):查询n条以后的数据
User.find().skip(3).then((result)=>{
console.log(result);
})

012、显示n-m之间的数据 skip:跳过n条 limit 显示m-n条
User.find().skip(3).limit(2).then((result)=>{
console.log(result)
})

【node】mongoose的基本使用的更多相关文章
- Node.mongoose
简介 mongodb是一款面向文档的数据库,不是关系型数据库,新手熟悉mysql.sqlserver等数据库的人可能入手稍微困难些,需要转换一下思想,可以不需要有固定的存储模式,以文档模型为存储内容相 ...
- vue+node+mongoose踩过的坑
1.当你在cmd中输入npm run dev的时候,出现这种错误 很有可能是目前的端口被占用了,可以把所有可能用到这个端口号的应用关闭或者你直接改一个新的端口号 修改端口的方法:新打开一个cmd,然后 ...
- node+mongoose使用例子
https://github.com/Aquarius1993/nodeNotes 功能 1. 注册 2. 登录 3. 修改密码 4. 修改头像 5. 获取用户笔记 6. 添加,删除,更新笔记 安装部 ...
- node+mongoose+vue
app.js 入门 let express = require('express'); let app = express(); let allowCrossDomain = function (re ...
- mongoose的promise(转发)
Switching out callbacks with promises in Mongoose Published on July 28, 2015 mongo node mongoose pro ...
- node.js学习的资源整理
node中文社区 Node.js专业中文社区:https://cnodejs.org/ node文档 node.js 中文api :http://nodeapi.ucdok.com/ node.js入 ...
- [Mongo] 解决mongoose不支持条件操作符 $gt$gte:$lte$ne $in $all $not
reference : http://blog.sina.com.cn/s/blog_4df23d840100u25x.html 找到mongoose的安装目录 /usr/local/lib/node ...
- MongoDB 驱动以及分布式集群读取优先级设置
本文主要介绍使用MongoDB C驱动读取分布式MongoDB集群时遇到的坑,主要在读取优先级和匹配tag上:同时简单介绍Python驱动.Node.js驱动.Mongoose驱动如何使用读取优先级和 ...
- nodejs mongodb 查询要看的文章
http://www.cnblogs.com/refactor/archive/2012/07/30/2591344.html 数组很大多数情况下可以这样理解:每一个元素都是整个键的值. db.use ...
- vue的项目初始化
1.创建文件 blog 2.下载安装node mongoose 3.(1)vue创建后端项目文件 vue create admin (2)vue创建前端项目文件 vue create web (3)新 ...
随机推荐
- 使用iperf测试网卡吞吐性能
原 使用iperf测试网卡吞吐性能 2018年12月17日 12:38:41 lancewoo 阅读数:138 首先配置待测试的两个网卡的网络地址到同一网段,保证ping对方的IP地址时可以通.两 ...
- vue把localhost改成ip地址无法访问—解决方法
打开package.json文件,找到下面的代码 "scripts": { "dev": "webpack-dev-server --inline - ...
- bootstrap-table方法之:合并单元格
方法一 通过mergeCells方法 演示地址:http://issues.wenzhixin.net.cn/bootstrap-table/methods/mergeCells.html Merge ...
- Python 读取 支付宝账单并存储到 Access 中
我有一个很多年前自己写的C#+Access的记账程序,用了很多年,现在花钱的机会多了,并且大部分走的支付宝,于是就想把账单从支付宝网站上下载下来,直接写入到Access,这样就很省心了. 记账程序是长 ...
- Project with Match in aggregate not working in mongodb
[问题] 2down votefavorite I am trying to fetch data based on some match condition. First I've tried th ...
- 市场风险~VaR的概述
1.概念理解 VaR的含义:Value at Risk 按字面的解释就是"处于风险状态的价值",可译为受险价值.在险价值.风险价值等. 通常解释为:VaR是在一定置信水平和一定持有 ...
- iOS 内购讲解
一.总说内购的内容 1.协议.税务和银行业务 信息填写 2.内购商品的添加 3.添加沙盒测试账号 4.内购代码的具体实现 5.内购的注意事项 二.协议.税务和银行业务 信息填写 2.1.协议.税务和银 ...
- 【C#】C#线程_计算限制的异步操作
目录结构: contents structure [+] 线程池简介 执行上下文(Execution Context) CancelTokenSource的使用 ThreadPool Task和Tas ...
- Java之收集很好的Java学习资料地址+博客
https://blog.insanecoder.top/tcp-packet-splice-and-split-issue/ http://blog.csdn.net/qilixiang012/ar ...
- 关键词提取算法TextRank
很久以前,我用过TFIDF做过行业关键词提取.TFIDF仅仅从词的统计信息出发,而没有充分考虑词之间的语义信息.现在本文将介绍一种考虑了相邻词的语义关系.基于图排序的关键词提取算法TextRank. ...