zoukankan      html  css  js  c++  java
  • mongoose为字段添加索引

    MongoDB在读取数据时,如果没有索引,会扫描集合中的每个文件并选取那些符合查询条件的记录。

    添加合适的索引能够极大的提高查询的效率

    let fileStatus = new Schema({
        materials: String,
        geometries: String,
        guid: {
            type: String,
            index: true,
        }, 
        size: Number,
        path: String,
    });

    添加后启动node服务可能会出现如下提醒

    (node:9764) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

    解决的方法:

    在连接前添加

    mongoose.set("useCreateIndex", true);
    mongoose.connect(link, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        poolSize: 1000,
    });

    查看索引

    > db.getCollection("status").getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "abc.status"
            },
            {
                    "v" : 2,
                    "key" : {
                            "guid" : 1
                    },
                    "name" : "guid_1",
                    "ns" : "abc.status",
                    "background" : true
            }
    ]
  • 相关阅读:
    Prototype
    Builder Pattern
    Chain of Responsibility
    Flyweight
    HBase概念学习(九)HTablePool为何弃用?
    用web查看hadoop运行状态
    Hadoop的位置
    SQLServer的TDE加密
    Log4Net advanced pattern tips
    Forrest Gump
  • 原文地址:https://www.cnblogs.com/baby123/p/13492098.html
Copyright © 2011-2022 走看看