gpt4 book ai didi

javascript - Mongoose /Mongodb : Index Already Exists With Different Options

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:21 25 4
gpt4 key购买 nike

我正在使用 Mongoose 5.1.7 并尝试在我定义的架构中跨多个文本索引创建复合索引。这是我的架构定义:

const mongoose = require('mongoose');

const alumniSchema = mongoose.Schema({
firstName: {
type: [String],
required: true
},
lastName: {
type: [String],
required: true
},
classYear: {
type: Number,
required: true
},
photoURL: {
type: String,
},
education: [
{
school: {
type: String,
required: true
},
gradYear: {
type: Number,
required: true
},
degreeType: String,
degreeSubject: String,
}
],
jobs: [
{
employer: {
type: String,
required: true
},
position: String,
startDate: Date,
endDate: Date,
isCurrent: Boolean
}
],
contactInfo: {
phoneNumber: {
type: String,
},
email: {
type: String,
}
},
})

alumniSchema.index({ firstName: 'text', lastName : 'text', email: 'text' });

module.exports = mongoose.model('Alumni', alumniSchema);

当我启动服务器时,收到以下错误:

MongoError: Index: { v: 2, key: { _fts: "text", _ftsx: 1 }, name: "firstName_text_lastName_text_email_text", ns: "5b3be578c0c6e317f7c1bc2b_test.alumnis", background: true, weights: { email: 1, firstName: 1, lastName: 1 }, default_language: "english", language_override: "language", textIndexVersion: 3 } already exists with different options: { v: 2, key: { _fts: "text", _ftsx: 1 }, name: "firstName_text_lastName_text_classYear_text_education.school_text", background: true, weights: { classYear: 1, education.school: 1, firstName: 1, lastName: 1 }, default_language: "english", language_override: "language", ns: "5b3be578c0c6e317f7c1bc2b_test.alumnis", textIndexVersion: 3 }

我已经在搞这个问题有一段时间了,显然之前创建了一个索引。但是,当我使用 mongo shell 检查当前设置的索引时,找不到错误消息引用的索引“firstName_text_lastName_text_classYear_text_education.school_text”:

> db
test
> db.collection.getIndexes()
[ ]

我陷入了僵局——我不确定我是否错误地创建了索引,或者我是否应该删除索引(看起来 Mongoose 本身并不支持 dropIndex() 函数)。

还有人处理过这个问题吗?谢谢!

最佳答案

看起来 Mongoose 在运行时动态创建索引。对我来说,诀窍是添加:

var Alumni = mongoose.model('Alumni', alumniSchema);
Alumni.collection.dropIndex('firstName_text_lastName_text_classYear_text_education.school_text', function(err, result) {
if (err) {
console.log('Error dropping index!', err);
}
});

然后重新启动服务器。

然后我就可以将索引更改为我想要的任何内容。请注意,每次想要更新索引时,我仍然需要添加上述代码段并重新启动服务器。

关于javascript - Mongoose /Mongodb : Index Already Exists With Different Options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com