【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)新 ...
随机推荐
- React组件间的通信
1.子组件调用父组件,采用props的方式进行调用和赋值,在父组件中设置相关属性值或者方法,子组件通过props的方式进行属性赋值或者方法调用: 2.父组件调用子组件,采用refs的方式进行调用,需要 ...
- mac双系统下ubuntu卡在开机密码登录界面卡死
背景:开机黑屏卡在,dev/sda1: clean, 552599/6111232 files, 7119295/24414464 blocks,修复完这个问题之后,出现桌面卡死,光标鼠标键盘全部失灵 ...
- [CSS] Useful CSS tool for Web designer and developer
1. Color Picker (Chrome) You might know how to use color picker in Chrome, recently there is a featu ...
- FutureTask类
FutureTask类是Future 的一个实现,并实现了Runnable. 所以可通过Executor(线程池)来运行,也可传递给Thread对象运行. 假设在主线程中须要运行比較耗时的操作时.但 ...
- Autohotkey window 下宏键盘、宏命令开发入门
- 企业级镜像仓库Harbor
介绍: Habor是由VMWare公司开源的容器镜像仓库.事实上,Habor是在Docker Registry上进行了相应的企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:管理用户界面, ...
- Django Rest Framework(认证、权限、限制访问频率)
阅读原文Django Rest Framework(认证.权限.限制访问频率) django_rest_framework doc django_redis cache doc
- 【转】使用minizip解压缩多个文件(基于zlib)
原帖子:使用minizip解压缩多个文件(基于zlib) 写作目的:之前在网上看到很多人在寻找可以解压缩多个文件的程序,其中有尝试zlib的,使用zlib的源码可以生成后缀为点gz的压缩文件,但是一次 ...
- 如何关闭windows server2012 80端口
Windows Server禁用本地端口的两种方法 这篇文章主要介绍了Windows Server 2008 禁用本地端口的两种方法,本文讲解了通过Windows防火墙禁用端口.通过IP安全策略禁用端 ...
- const的位置问题
来源:牛客网 下列哪两个是等同的 int b; 1.const int *a = &b; 2.const * int a = &b; 3.const int* const a = &a ...