node.js - mongoose 设置架构索引在版本更新后抛出异常,属性“key”丢失但在“IndexSpecification”类型中是必需的
问题描述
我曾经使用以下语法设置架构索引:
mySchema.index({ name: 'text' });
我name
的架构的属性也是如此。
猫鼬更新后,我收到以下错误:
Argument of type '{ name: string; }' is not assignable to parameter of type 'IndexSpecification'.
Property 'key' is missing in type '{ name: string; }' but required in type 'IndexSpecification'.ts(2345)
我应该在这里设置什么键?
猫鼬版本:
mongoose@^5.13.6
@types/mongoose@^5.11.97
谢谢!
解决方案
按照 mongodb 文档,
钥匙:
指定索引的字段。对于每个字段,指定一个键值对,其中键是要索引的字段的名称,值是索引方向或索引类型。如果指定方向,指定 1 表示升序或 -1 表示降序。
所以答案是:
mySchema.index({ key: { name: 1 }, name: 'nameIndex' });
推荐阅读
- apache-spark - Apache Spark 如何对大文件的数据进行分区
- java - 我可以在 Web 视图(android)中将 Applet 放入 html 代码中吗
- python - 调用存储函数或过程不会插入和保留更改
- javascript - 根据所选选项 javascript 动态更改跨度文本
- javascript - React:第一次,未定义的错误,但重新加载后,它成功
- python - UnicodeDecodeError:“utf8”编解码器无法解码位置 51 中的字节 0x89:无效的起始字节
- mysql - 将 int 从类插入数据库(mysql)
- vue.js - 计算属性未定义,虽然它是......定义 - vue.js
- npm - 通过 npm 使用 socket.io 时 HTML 中出现“Unexpected Token <”错误
- javascript - 切换图标仅适用于一项