Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] user is not allowed to do action [createIndex] on [db.system.indexes] #6976

Closed
kimmanwky opened this issue Sep 5, 2018 · 5 comments
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity

Comments

@kimmanwky
Copy link

I'm working on project in node.js + express with mongoose, connected to Mongodb Atlas (free sandbox).

Using:
"mongoose": "^5.1.8",

With user model schema as below:
username: { type: Types.String, required: true, unique: true },

I hit this error when connecting to mongodb altas sandbox server.
MongoError: user is not allowed to do action [createIndex] on [tp2fa.system.indexes]

Based on my research, mongodb altas doesnt not allow user to createindex at db.system, and indexes should be created on collection [db.collection.indexes].

Is this a bug or there is a way for us to create indexes on specific collection to avoid such error when using mongodb altas?

Note:
The user that used to connect to mongodb altas is Altas Admin (full permission)

Reference:

https://stackoverflow.com/questions/49958635/mongodb-atlas-user-is-not-allowed-to-do-action-find-on-system-indexes/52176428?noredirect=1#comment91303243_52176428

@kimmanwky kimmanwky changed the title [BUG] user is not allowed to do action [createIndex] on [db.system.indexes] [QUESTION] user is not allowed to do action [createIndex] on [db.system.indexes] Sep 5, 2018
@ghost
Copy link

ghost commented Sep 6, 2018

@kimmanwky I believe this was fixed in a past update. Can you update to mongoose@latest and try again? Be aware that with the newest version, you'll need to deal with some deprecation warnings as detailed here and index creation failing with retryWrites=true in your connection string as detailed here.

A simple example:

6976.js

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

const { ATLASSRV } = require('/Users/lineus/.env');
const mongoose = require('mongoose');
let uri;
let opts = {};

if (mongoose.version === '5.2.13') {
  opts.useNewUrlParser = true;
  mongoose.set('useCreateIndex', true);
  uri = ATLASSRV.replace('?retryWrites=true', '');
} else {
  console.log(`running mongoose: ${mongoose.version}`);
  uri = ATLASSRV;
}

mongoose.connect(uri, opts);
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({ name: { type: String, unique: true } });
const Test = mongoose.model('Test', schema);

async function run() {
  await conn.dropDatabase();
  await Test.init();
  console.log(`it works in mongoose: ${mongoose.version}`);
  return conn.close();
}

run();

Output with 5.1.8

issues: ./6976.js
running mongoose: 5.1.8

/Users/lineus/dev/Help/mongoose5/node_modules/mongoose/lib/utils.js:452
        throw err;
        ^
MongoError: user is not allowed to do action [createIndex] on [test.system.indexes]
    at /Users/lineus/dev/Help/mongoose5/node_modules/mongodb-core/lib/connection/pool.js:593:63
    at authenticateStragglers (/Users/lineus/dev/Help/mongoose5/node_modules/mongodb-core/lib/connection/pool.js:516:16)
    at Connection.messageHandler (/Users/lineus/dev/Help/mongoose5/node_modules/mongodb-core/lib/connection/pool.js:552:5)
    at emitMessageHandler (/Users/lineus/dev/Help/mongoose5/node_modules/mongodb-core/lib/connection/connection.js:309:10)
    at TLSSocket.<anonymous> (/Users/lineus/dev/Help/mongoose5/node_modules/mongodb-core/lib/connection/connection.js:452:17)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at TLSSocket.Readable.push (_stream_readable.js:208:10)
    at TLSWrap.onread (net.js:607:20)
issues:

Output with 5.2.13 (mongoose@latest)

issues: ./6976.js
it works in mongoose: 5.2.13
issues:

@vkarpov15
Copy link
Collaborator

As a workaround, you can also use the autoIndex option to turn off Mongoose's automatic index builds. mongoose.connect(uri, { autoIndex: false }).

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Sep 10, 2018
@ghost
Copy link

ghost commented Oct 3, 2018

Let us know if this is still an issue for you @kimmanwky, closing this for now.

@SabrinaDanielle
Copy link

@kimmanwky did you ever resolve your issue? I have been trying to resolve this for hours.

@kimmanwky
Copy link
Author

@SabrinaDanielle I'm afraid I not quite remember how do I fix it. Perhaps, you could try by adding additional options for your MongoDB connection?

"useCreateIndex": true

image

@Automattic Automattic locked as resolved and limited conversation to collaborators Dec 20, 2019
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants