question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when trying to change existing text index

See original GitHub issue
Error: 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:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Sep 3, 2016
0reactions
lineuscommented, Sep 13, 2018

@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

#!/usr/bin/env node
'use strict';

const { MongoClient } = require('mongodb');
const URI = 'mongodb://localhost:27017/gh4459';
const OPTS = { useNewUrlParser: true };

async function run() {
  let client = await MongoClient.connect(URI, OPTS);
  let Tests = client.db('gh4459').collection('tests');
  await client.db('gh4459').dropDatabase();
  await Tests.createIndex({ name: 1 }, { name: 'blarghfooey' });
  return client.close();
}

run().catch(console.error);

4459.js

#!/usr/bin/env node
'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
mongoose.connect('mongodb://localhost:27017/gh4459', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  message: String,
  name: String,
  email: String
});

const index = { message: 'text', name: 'text', email: 'text' };
const iOpts = { name: 'blarghfooey', background: false };

schema.index(index, iOpts);

async function run() {
  const Test = mongoose.model('test', schema);
  await Test.syncIndexes().catch(console.error);
  let indexes = await Test.listIndexes();
  console.log(indexes);
  return conn.close();
}

run();

Output:

issues: ./4459_before.js
issues: ./4459.js
[ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'gh4459.tests' },
  { v: 2,
    key: { _fts: 'text', _ftsx: 1 },
    name: 'blarghfooey',
    ns: 'gh4459.tests',
    background: false,
    weights: { email: 1, message: 1, name: 1 },
    default_language: 'english',
    language_override: 'language',
    textIndexVersion: 3 } ]
issues:

if you change syncIndexes() to createIndexes() this example will fail as you described CAUTION: syncIndexes() will remove all indexes not defined in your schema

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found