Personhere
Personhere

Reputation: 13

How to make a MongoDB index with using a variable in Node.js?

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.

Upvotes: 1

Views: 183

Answers (1)

cheekujha
cheekujha

Reputation: 801

In es6

createIndex({[x]:1})

In es5

var indexQuery = {};
indexQuery[x] = 1;
createIndex(indexQuery);

Upvotes: 1

Related Questions