一,局部安装mongoose

npm install mongoose -S

二、mongoose常用命令:

//引入mongoose模块:
const mongoose = require('mongoose');//连接数据库:
mongoose.connect('mongodb://127.0.0.1:27017/kinoko',(err)=>{if(err){console.log('连接失败')}else{console.log('连接成功')}//创建表以及字段类型://创建表userconst User = mongoose.model('user',{//规定user表中的字段类型:
        name:String,age:Number})//增:const user = new User({name : '阿古',age:19})const user1 = new User({name : '美丽',age:22})const user2 = new User({name : '笑笑',age:21})const user3 = new User({name : '刘梅',age:56})console.log(user.save())  //输出:Promise{<pending>}user.save().then((result)=>{console.log('成功的回调')},()=>{console.log('失败的回调')})user1.save().then((result)=>{console.log('成功的回调')},()=>{console.log('失败的回调')})user2.save().then((result)=>{console.log('成功的回调')},()=>{console.log('失败的回调')})user3.save().then((result)=>{console.log('成功的回调')},()=>{console.log('失败的回调')})//删:
//删除指定对象:
User.remove({name:'美丽'}).then((result)=>{ //result:是一个对象 返回值是受影响条数
    console.log(result)
})//删除所有数据:
User.remove({}).then((result)=>{console.log(result)
})//删除指定ID
User.findByIdAndRemove("5c8263170998c51d58e14044").then((result)=>{   //5c8263170998c51d58e14044 ID值
    console.log(result);
})//改:
User.update({name:'阿古'},{$set:{name:"lily"}},{multi:true}).then((result)=>{console.log(result);    //multi:true  表示修改多条数据
})User.findByIdAndUpdate("5c8263170998c51d58e14046",{$set:{name:'rose'}},{multi:true}).then((result)=>{console.log(result);
})//查找符合条件的所有数据:
User.find({name:'lily'}).then((result)=>{console.log(result);
})// //查询所有数据:
User.find().then((result)=>{console.log(result);
})//查询单条数据:
User.findOne({name:'lily'}).then((result)=>{console.log(result);
})//条件查询:$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':20}}).then((result)=>{console.log(result);
})//$in(包含等于)       $nin(不包含 不等于)
User.find({'age':{'$in':[18,19]}}).then((result)=>{console.log(result)})User.find({'age':{'$nin':[18,19]}}).then((result)=>{console.log(result)})$exists(判断当前关键字是否存在)User.find({sex:{'$exists':true}}).then((result)=>{console.log(result);})//查询指定列  如果不想要ID值  只需要设置ID = 0;
User.find({},{name:1,_id:0}).then((result)=>{console.log(result);
})//'$or'或
User.find({'$or':[{name:'lily'},{age:19}]}).then((result)=>{console.log(result);
})//$exists(判断当前关键字是否存在)
User.find({name:{'$exists':'true'}}).then((result)=>{console.log(result)
})//升序降序
User.find().sort({age:1}).then((result)=>{console.log(result)
})User.find().sort({age:-1}).then((result)=>{console.log(result)
})//模糊查询:
User.find({name:/l/}).then((result)=>{console.log(result)
})User.find({name:/^l/}).then((result)=>{   //以l开头
    console.log(result)
})User.find({name:/l$/}).then((result)=>{   //以l结尾
    console.log(result)
})//skip()查询n条以后的数据
User.find().skip(1).then((result)=>{console.log(result)
})//显示n到m之间的数据 skip跳过多少条,limit显示m-n条
User.find().skip(1).limit(3).then((result)=>{console.log(result);
})
})

转载于:https://www.cnblogs.com/kinoko-1009/p/10499663.html

mongoose的操作及其常用命令相关推荐

  1. Java中使用Jedis连接Redis对Hash进行操作的常用命令

    场景 Centos中Redis的下载编译与安装(超详细): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334 Re ...

  2. Java中使用Jedis连接Redis对Set进行操作的常用命令

    场景 Centos中Redis的下载编译与安装(超详细): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334 Re ...

  3. Java中使用Jedis连接Redis对List进行操作的常用命令

    场景 Centos中Redis的下载编译与安装(超详细): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334 Re ...

  4. Java中使用Jedis连接Redis对String进行操作的常用命令

    场景 Centos中Redis的下载编译与安装(超详细): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334 Re ...

  5. java向hdfs提交命令_Java语言操作HDFS常用命令测试代码

    本文主要向大家介绍了Java语言操作HDFS常用命令测试代码,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助. package com.yxc.hdfs; import org.apac ...

  6. Redis String 类型操作及常用命令

    七个原则 Redis 是一个操作数据结构的语言工具,它提供基于 TCP 的协议以操作丰富的数据结构.在 Redis 中,数据结构这个词的意义不仅表示在某种数据结构上的操作,更包括了结构本身及这些操作的 ...

  7. Redis set 类型操作及常用命令

    七个原则 Redis 是一个操作数据结构的语言工具,它提供基于 TCP 的协议以操作丰富的数据结构.在 Redis 中,数据结构这个词的意义不仅表示在某种数据结构上的操作,更包括了结构本身及这些操作的 ...

  8. Redis Hash 类型操作及常用命令

    七个原则 Redis 是一个操作数据结构的语言工具,它提供基于 TCP 的协议以操作丰富的数据结构.在 Redis 中,数据结构这个词的意义不仅表示在某种数据结构上的操作,更包括了结构本身及这些操作的 ...

  9. Redis List 类型操作及常用命令

    七个原则 Redis 是一个操作数据结构的语言工具,它提供基于 TCP 的协议以操作丰富的数据结构.在 Redis 中,数据结构这个词的意义不仅表示在某种数据结构上的操作,更包括了结构本身及这些操作的 ...

最新文章

  1. C语言之结构体以及结构体对齐访问
  2. CentOS 开机自启动脚本
  3. [编程题]数字分类 (20)
  4. 机器学习中激活函数和模型_探索机器学习中的激活和丢失功能
  5. ASP.NET+AJAX简单的文体编辑器
  6. php 几个比较实用的函数
  7. winform 图片压缩大小为原图的一半_SCI论文图片编辑——常见十问十答
  8. 《3D数学基础系列视频》1.2向量的数乘和加减法
  9. 高等数学所有符号的写法与读法
  10. 手机12306买卧铺下铺技巧_12306订下铺有什么技巧?
  11. 腾讯云学生服务器还不如直接选轻量应用服务器
  12. 要想Essay写得好,切记要警惕这六大禁区
  13. STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解)
  14. 利用D盘内存给C盘扩容
  15. MindSpore布道师招募计划,开启AI的信仰之跃
  16. 全面认识痛风:症状、风险因素、发病机理及管理
  17. 服务器修改host的ip,主机IP地址设置
  18. 徽章系列3: Travis CI 的使用
  19. Synctoy定时自动同步数据
  20. 基于WMS/WCS与PLC数据交互的立体仓库控制系统案例分析

热门文章

  1. 前端集成方案——理论(一)
  2. node.js中的文件系统
  3. jsp,jstl checkbox 回显方法
  4. 各大排序算法的Objective-C实现以及图形化演示比较
  5. 深入实践Spring Boot2.4.3 节点实体持久化
  6. SVN: bdb: BDB1538 Program version 5.3 doesn't match environment version 4.7
  7. 数据创建表 修改列 新增列
  8. Mac下sudo后环境变量失效的问题
  9. gradle学习(19)-log系统
  10. VC6.0设置注释快捷键