Error when trying to change existing text index
See original GitHub issueError: key name.first must not contain '.'
at Error (native)
at serializeInto (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:682:19)
at serializeObject (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:280:18)
at serializeInto (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:705:17)
at serializeObject (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:280:18)
at serializeInto (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:551:17)
at serializeObject (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:280:18)
at serializeInto (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:705:17)
at serialize (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/bson/lib/bson/bson.js:47:27)
at Query.toBin (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/commands.js:146:25)
at executeSingleOperation (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1214:31)
at Server.command (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1295:3)
at executeWrite (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:61:12)
at WireProtocol.insert (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:69:3)
at Server.insert (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1323:37)
at Server.insert (/private/tmp/30c4196e58d47d07499cf6403833b2bd/node_modules/mongoose/node_modules/mongodb/lib/server.js:351:17)
Reproduce with:
const Mongoose = require('mongoose');
Mongoose.Promise = Promise;
Mongoose.connect(`mongodb://localhost/test-${Date.now()}`);
const PersonSchema = new Mongoose.Schema({
name: {
first: { type: String },
last: { type: String }
}
});
// Set up first version of the text index.
PersonSchema.index({ 'name.first': 'text' });
const Person = Mongoose.model('Person', PersonSchema);
Person.ensureIndexes()
.then(() => {
// Set up new version of text index.
PersonSchema.index({ 'name.first': 'text', 'name.last': 'text' });
return Person.ensureIndexes();
})
.then(() => {
console.log('It worked.');
process.exit();
})
.catch(error => {
console.error(error.stack);
process.exit(1);
});
Also in this gist.
In the script I declare Model.index()
twice, calling Model.ensureIndexes()
in between. It emulates what you do when you are changing an existing index. Running this as separate steps in separate scripts yields the same result.
Mongoose: 4.5.10 MongoDB: 3.2.9 Node.js: 4.4.6
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
Top Results From Across the Web
Error when using a text index on Mongodb - Stack Overflow
This error occurs when you try to create a text index on a field in a document, and the document has a field...
Read more >IBM Content Manager DB2 Text Search - Error occurs when ...
During DB2 Text Search text index creation, the error SQL -20427 occurs due to a failure of DB2 Text Search text index creation....
Read more >Could not dropped text index - Working with Data - MongoDB
I am trying to change the text index setting, but the existing text index is not deleted. Reproduce A text index is created...
Read more >Create and update an index - Microsoft Support
If you find an error in the index, locate the index entry that you want to change, make the change, and then update...
Read more >5 Maintaining Oracle Text Indexes
You must drop an existing index before you can re-create it with the CREATE INDEX statement. Drop an index by using the DROP...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Waiting on changes from https://github.com/mongodb/node-mongodb-native/commit/052b2e3f6f30a797f0cd44c14c89d80b5796b73b to get released
@elena-dev, If you are trying to add an index to an existing collection, it must have a unique name.
You can use the
Model.syncIndexes()
command to remove all indexes from your collection that don’t match the ones in your schema, or if you want to keep both indexes you can rename the new one.for example:
4459_before.js
4459.js
Output:
if you change syncIndexes() to createIndexes() this example will fail as you described CAUTION: syncIndexes() will remove all indexes not defined in your schema