1

I am using createIndex() in MongoDB in Node.js. I was wondering how I could insert a variable into createIndex.

For instance I have var x = "insert". I would like to make an index called "insert" by using x. I have tried createIndex({x:1}) but all this does is make x the index.

1 Answer 1

1

In es6

createIndex({[x]:1})

In es5

var indexQuery = {};
indexQuery[x] = 1;
createIndex(indexQuery);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you forgot the exact syntax your a life saver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.