【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) }) //删除指定ID3、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) })
转载于:https://www.cnblogs.com/nanianqiming/p/9069760.html
【node】------mongoose的基本使用------【巷子】相关推荐
- node+mongoose使用例子
https://github.com/Aquarius1993/nodeNotes 功能 1. 注册 2. 登录 3. 修改密码 4. 修改头像 5. 获取用户笔记 6. 添加,删除,更新笔记 安装部 ...
- mongooes怎么链接mysql_如何使用Node + Mongoose连接远程MongoDB数据库
POPMUISE 1.基于mongo实现远程连接[plain] view plaincopymongo -u admin -p admin 192.168.0.197:27017/pagedb通过mo ...
- node mongoose_如何使用Express,Mongoose和Socket.io在Node.js中构建实时聊天应用程序
node mongoose by Arun Mathew Kurian 通过阿伦·马修·库里安(Arun Mathew Kurian) 如何使用Express,Mongoose和Socket.io在N ...
- node.js学习的资源整理
node中文社区 Node.js专业中文社区:https://cnodejs.org/ node文档 node.js 中文api :http://nodeapi.ucdok.com/ node.js ...
- 打造前端瑞士军刀,为你开发路上披荆斩棘
每个工具都可以点击查看打开链接,因为微信内部不能查看外链,所以大家可以点击查看原文打开外链后进行查看 主要是为了帮助大家搜集一下前端开发中要用到的一些东西,打造一个前端开发工具字典,方便大家查阅. 篇 ...
- 前端工具字典,为你开发路上披荆斩棘
路漫漫其修远兮,吾将上下而求索~ 此文为转载文章,已获得作者同意 主要是为了帮助大家搜集一下前端开发中要用到的一些东西,打造一个前端开发工具字典,方便大家查阅. 篇幅比较长,先给大家放一张脑图,看看自 ...
- 快速入门nodejs-前端必会
NodeJS 介绍 目标 优势 劣势 环境安装 版本 node命令行 运行 window 苹果 vscode webstrom 开发注意 web服务器 构成 前后端交互流程 实现 fs模块 读取 更名 ...
- 使用nodejs编写静态服务器
1.使用nodejs编写静态服务器 const http = rquire('http'); const url = require('url'); const server = http.creat ...
- 01 硅谷外卖项目day01
README.md 项目介绍 gshop-client 硅谷外卖前台 gshop-server 硅谷外卖后台 gshop-client_pages 硅谷外卖切图 技术参考 前台 axios bette ...
最新文章
- 湘潭大学计算机学院调剂,湘潭大学2018年硕士研究生调剂信息公告
- Hadoop上路_11-HDFS流程演示
- 程序员面试100题之十六:二叉树中两个节点的最近公共父节点(最低的二叉树共同祖先)
- ASP.NET MVC V2 Preview 1 发布 期望VS有更好的表现
- 注意html的语言编码charset,HTML编码
- 去掉IE的图片工具条
- Tarjan缩点简析
- C语言实现俄罗斯方块
- 斗鱼显示弹幕服务器连接失败,斗鱼看不到弹幕怎么办 斗鱼无法看到弹幕的解决方法...
- Hash表的时间复杂度为什么是O(1)?
- 系列学习 Lambda 表达式之第 2 篇 —— JDK1.8 的 Stream 流基本使用
- python编程自然数表达式_结对编程-python实现
- mtu设置失败_华为路由器修改MTU值失败怎么办
- python 方向键控制_python方向键控制上下左右代码
- adg的archive出现gap,使用增量恢复的方式进行恢复adg,RMAN-06094: datafile 1 must be restored
- 最简单的基于DirectShow的示例:视频播放器
- 微星a320m主板可以装服务器系统吗,技嘉a320m主板:微星a320m主板,正式再见
- 甲骨文考虑收购市值775亿美元的埃森哲
- 西北工业大学计算机学院张磊,西工大校友张磊当选2018年IEEE Fellow
- DameWare Mini远程连接报错信息处理方法。