Mongoose 索引介绍

索引是对数据库表中一列或多列的值进行排序的一种结构,可以让我们查询数据库变得更 快(如果有些字段是用不着的就不要设置索引)。MongoDB 的索引几乎与传统的关系型数据库一模一样,这其中也包括一些基本的查询 优化技巧。

mongoose 中除了以前创建索引的方式,我们也可以在定义 Schema 的时候指定创建索引

Mongoose 索引的使用

先初始化项目还是一个app.js和一个model文件夹,分别由db.js(连接数据库),users.js(操作users集合的Schema)

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/eggcms', { useNewUrlParser: true }, (err) => {
if(err){
return console.log(err);
}
console.log('数据库连接成功')
}); module.exports = mongoose
let mongoose = require('./db')

let UserSchema = mongoose.Schema({
name: {
type: String,
trim: true
},
age: Number,
status: {
type: Number,
default: 1
}
}) // 定义model操作数据库
let UserModel = mongoose.model('User',UserSchema,'users'); module.exports = UserModel
let UserModel = require('./model/users')

// 查询users表的数据
UserModel.find({}, (err, doc) => {
if (err) {
console.log(err)
return
}
console.log(doc)
})

基础搭建好之后,设置索引示在具体集合中的Schema中去设置,比如给users集合设置索引,就在users.js这个模块的Schema中设置

设置普通索引

let mongoose = require('./db')

let UserSchema = mongoose.Schema({
name: {
type: String,
trim: true,
index: true // 设置普通的索引
},
age: Number,
status: {
type: Number,
default: 1
}
}) // 定义model操作数据库
let UserModel = mongoose.model('User',UserSchema,'users'); module.exports = UserModel

然后再我们执行数据库操作(增删改查)的时候会检查这个集合有没有索引,如果没有就会增加

let UserModel = require('./model/users')

// 查询users表的数据
UserModel.find({}, (err, doc) => {
if (err) {
console.log(err)
return
}
console.log(doc)
})

运行app.js之后我们再去获取users的所有看看,结果是设置成功的

设置多个索引

let mongoose = require('./db')

let UserSchema = mongoose.Schema({
name: {
type: String,
trim: true,
index: true // 设置普通的索引
},
age: {
type: Number,
index: true // 设置普通的索引
},
status: {
type: Number,
default: 1
}
}) // 定义model操作数据库
let UserModel = mongoose.model('User',UserSchema,'users'); module.exports = UserModel

设置唯一索引,先删除之前设置的索引,再删除索引的时候需要注意,如果设置了多个索引(不是一起设置的,删除的时候也要分开删除)

db.users.dropIndex({"name":1})
db.users.dropIndex({"age":1})

let mongoose = require('./db')

let UserSchema = mongoose.Schema({
name: {
type: String,
trim: true,
unique: true // 设置唯一的索引
},
age: {
type: Number
},
status: {
type: Number,
default: 1
}
}) // 定义model操作数据库
let UserModel = mongoose.model('User',UserSchema,'users'); module.exports = UserModel
// db.users.dropIndex({"name":1})
// db.users.dropIndex({"age":1})

Mongoose 索引的更多相关文章

  1. MongoDB学习day08--Mongoose索引、Mongoose内置方法、扩展Mongoose Model的静态方法和实例方法

    一.Mongoose索引 索引是对数据库表中一列或多列的值进行排序的一种结构, 可以让我们查询数据库变得更快. MongoDB 的索引几乎与传统的关系型数据库一模一样, 这其中也包括一些基本的查询优化 ...

  2. MongoDB快速入门教程 (4.4)

    4.5.Mongoose索引和方法 4.5.1.设置索引 let UserSchema = mongoose.Schema({ sn: { type: Number, // 设置唯一索引 unique ...

  3. mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...

  4. mongoose中给字段添加索引的方法

    mongoose中给字段添加索引的方法有两种,一种通过在定义schema的时候配置,如: var animalSchema = new Schema({ name: String, type: Str ...

  5. mongoDB (mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    MongoDB - 简介 官网:https://www.mongodb.com/ MongoDB 是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为 WEB 应用提供可扩展的高性能数据存储 ...

  6. 【转】mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    mongoDB 学习笔记纯干货(mongoose.增删改查.聚合.索引.连接.备份与恢复.监控等等) http://www.cnblogs.com/bxm0927/p/7159556.html

  7. mongoose - 让node.js高效操作mongodb

    Mongoose库简而言之就是在node环境中操作MongoDB数据库的一种便捷的封装,一种对象模型工具,类似ORM,Mongoose将数据库中的数据转换为JavaScript对象以供你在应用中使用. ...

  8. Mongo基础使用,以及在Express项目中使用Mongoose

    MongoDB的基本使用 MongoDB特点: 使用BSON存储数据 支持相对丰富的查询操作(相对其他nosql数据库) 支持索引 副本集(支持多个实例/多个服务器运行同个数据库) 分片(数据库水平扩 ...

  9. mongoose 和 mongoDB

    第三方学习地址:http://blog.csdn.net/foruok/article/details/47746057 下载mongoDB https://www.mongodb.com/downl ...

随机推荐

  1. MySQL之简介以及数据类型(一)

    一:关系型数据库 所谓的关系型数据库RDBMS,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据. 二:关系型数据库的主要产品: oracle:在以前的大型项目中使用 ...

  2. 前端1-----CSS层叠样式表了解,css的引入方式,三大选择器(标签,类,id),高级选择器

    前端1-----CSS层叠样式表了解,css的引入方式,三大选择器(标签,类,id),高级选择器 一丶CSS简介    叠样式表(英文全称:Cascading Style Sheets)是一种用来表现 ...

  3. FineReport连接ApacheKylin

    1.前言 Apache Kylin™是一个开源的分布式分析引擎,提供Hadoop之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据,最初由eBay Inc. 开发并贡献至开源社区.它能在 ...

  4. Elasticsearch 、 Logstash以及Kibana 分布式日志

    搭建ELK日志分析平台(上)—— ELK介绍及搭建 Elasticsearch 分布式集群 ELK简介: ELK是三个开源软件的缩写,分别为:Elasticsearch . Logstash以及Kib ...

  5. net start mysql 失败提示“NET HELPMSG 3534”

    lz使用的window系统8.0.16版本的mysql,以下四步解决如上问题: 1. mysqld -remove 2. mysqld --initialize 3.mysqld -install(m ...

  6. Web前端2019面试总结3(东软集团面试题)

    严禁转载,严禁分享,只供私自鉴赏,请君悉知! 一:基础题 1.什么是margin塌陷?请写出至少三种解决margin塌陷的方法. 答:当两个盒子在垂直方向上设置margin值时,会出现一个有趣的塌陷现 ...

  7. openssl生成证书及签名

    第一步,生成私钥 $ openssl genrsa -out privatekey.pem 2048 查看生成的私钥内容 $ file privatekey.pem privatekey.pem: P ...

  8. win2003下安装python3.4 + pyspider

    昨天尝试了在win2003下安装python2.7.这个是文章地址:https://www.cnblogs.com/alpiny/p/11706606.html 但是程序跑了一晚上,发现有一点问题,是 ...

  9. [SourceInsight].source insight 使用技巧

    转自:https://www.veryarm.com/140428.html 1  开胃菜-初级应用 1.1  选择美丽的界面享受工作 虽然不能以貌取人,但似乎从来没有人责备以貌取软件的.SI的华丽界 ...

  10. Linux操作系统之用户权限,重定向,文件管理

    文件的权限 ls -al  ----->隐藏文件会以 .号开头 ls -ld :显示目录自身属性 ls -i 显示文件的索引号----每个文件都有一个对应的号码 ls -r 逆序显示 dr-xr ...