Search code examples
node.jsmongodbmongoosepm2mongodb-indexes

PM2/NodeJS API with mongoDB index problems


I'm trying to deploy multiple NodeJS rest APIs in the same Ubuntu VPS using PM2, those APIs are used to serve data from different MongoDB database in the same local hosted db server.

I do encounter some port problems, but i do change it in every application to solve the problem. After that a strange mongoDB error appears.

API listen on port 3001
Connected!
MongoError: Index with pattern: { _fts: "text", _ftsx: 1 } already exists with different options
    at /var/app/my-project/node_modules/mongodb-core/lib/connection/pool.js:593:63
    at authenticateStragglers (/var/app/my-project/node_modules/mongodb-core/lib/connection/pool.js:516:16)
    at Connection.messageHandler (/var/app/my-project/node_modules/mongodb-core/lib/connection/pool.js:552:5)
    at emitMessageHandler (/var/app/my-project/node_modules/mongodb-core/lib/connection/connection.js:309:10)
    at Socket.<anonymous> (/var/app/my-project/node_modules/mongodb-core/lib/connection/connection.js:452:17)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

Now, the API keeps restarting and slows the all system. Please can anyone guide me threw this, note that i'm just maintaining the app. i'm not the developer and i'm not so familiar with mongo databases.

Thanks


Solution

  • check on which field you have your text index defined. Right now mongodb allows only one text index per collection

    db.collection.getIndexes()
    

    You will get some thing like this

     {
            "v" : 1,
            "key" : {
                "_fts" : "text",
                "_ftsx" : 1
            },
            "name" : "desc_text",
            "ns" : "some.ns",
            "weights" : {
                "title" : 1
            },
            "default_language" : "english",
            "language_override" : "language",
            "textIndexVersion" : 2
        }
    

    now if you want to scope in other columns also to use this index simply drop this index

    db.collection.dropIndex('desc_text');
    

    and then recreate it by including all columns you want to be covered by text index,

    db.collection.createIndex({
        title:'text'
          ...
    });