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的基本使用------【巷子】的更多相关文章

  1. Node.mongoose

    简介 mongodb是一款面向文档的数据库,不是关系型数据库,新手熟悉mysql.sqlserver等数据库的人可能入手稍微困难些,需要转换一下思想,可以不需要有固定的存储模式,以文档模型为存储内容相 ...

  2. vue+node+mongoose踩过的坑

    1.当你在cmd中输入npm run dev的时候,出现这种错误 很有可能是目前的端口被占用了,可以把所有可能用到这个端口号的应用关闭或者你直接改一个新的端口号 修改端口的方法:新打开一个cmd,然后 ...

  3. node+mongoose使用例子

    https://github.com/Aquarius1993/nodeNotes 功能 1. 注册 2. 登录 3. 修改密码 4. 修改头像 5. 获取用户笔记 6. 添加,删除,更新笔记 安装部 ...

  4. node+mongoose+vue

    app.js 入门 let express = require('express'); let app = express(); let allowCrossDomain = function (re ...

  5. mongoose的promise(转发)

    Switching out callbacks with promises in Mongoose Published on July 28, 2015 mongo node mongoose pro ...

  6. node.js学习的资源整理

    node中文社区 Node.js专业中文社区:https://cnodejs.org/ node文档 node.js 中文api :http://nodeapi.ucdok.com/ node.js入 ...

  7. [Mongo] 解决mongoose不支持条件操作符 $gt$gte:$lte$ne $in $all $not

    reference : http://blog.sina.com.cn/s/blog_4df23d840100u25x.html 找到mongoose的安装目录 /usr/local/lib/node ...

  8. MongoDB 驱动以及分布式集群读取优先级设置

    本文主要介绍使用MongoDB C驱动读取分布式MongoDB集群时遇到的坑,主要在读取优先级和匹配tag上:同时简单介绍Python驱动.Node.js驱动.Mongoose驱动如何使用读取优先级和 ...

  9. nodejs mongodb 查询要看的文章

    http://www.cnblogs.com/refactor/archive/2012/07/30/2591344.html 数组很大多数情况下可以这样理解:每一个元素都是整个键的值. db.use ...

  10. vue的项目初始化

    1.创建文件 blog 2.下载安装node mongoose 3.(1)vue创建后端项目文件 vue create admin (2)vue创建前端项目文件 vue create web (3)新 ...

随机推荐

  1. [oracle] 系统权限管理

    1 利用超级用户连接到数据库登录 sqlplus / as sysdba or slqplus SYS/SYSPWD as sysdba 这两个命令的效果是一样的,都是以sysdba的身份使得SYS管 ...

  2. C++ 基本的输入输出

    C++ 基本的输入输出C++ 标准库提供了一组丰富的输入/输出功能,我们将在后续的章节进行介绍.本章将讨论 C++ 编程中最基本和最常见的 I/O 操作. C++ 的 I/O 发生在流中,流是字节序列 ...

  3. linux -- Apache执行权限

    最近在用php调用exec方法去执行一个linux终端下的命令,结果每次都不能执行成功,网上多番搜寻,最终找到一篇有用的文章,主要原因是因为Apache的执行权限的问题.以下是原文(稍加修改): 利用 ...

  4. Windows下基于eclipse的Storm应用开发与调试

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3971113.html 本文以一个简单的example来讲解如何开发storm应用程序 1.创建maven ...

  5. 【转】Internet与Intranet区别

    提起Internet,大家都知道它是一个蓬勃发展的国际互联网. 而Intranet则是近两年才发展起来的新事物,通常被称作企业内部网. Internet是一组全球范围内信息资源的名字.这些资源非常巨大 ...

  6. 理解ASP.NET MVC中的ActionResult

    通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...

  7. Maven------pom.xml自动加载各种类库代码

    转载: http://lavasoft.blog.51cto.com/62575/1388866/ 一般要加<type>jar</type> <dependency> ...

  8. POJ 1426 Find The Multiple(背包方案统计)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  9. hadoop程序MapReduce之DataSort

    需求:对文件中的数据进行排序. 样本:sort.log 10 13 10 20 输出:1 10 2 10 3 13 4 20 分析部分: mapper分析: 1.<k1,v1>k1代表:行 ...

  10. stl中的map经验

    如果想使用一个map临时变量装载参数map,不需要使用new创建一个对象. 声明一个变量,直接赋值就可以.map内部自己重载了=操作符,会自己分配内存.