0

To enable unique index in node I do:

City.native(function(err, collection) {
    collection.ensureIndex({
        'name': 1,
    }, function(err, result) {
        //nothing
    });
});

But I would like to enable text index on name also. So after doing the above I do:

City.native(function(err, collection) {
    collection.ensureIndex({
        'name': 'text'
    }, function(err, result) {
        //nothing
    });
});

This perfectly creates both indices. My question is, is there any chance to merge this code?? I tried with

City.native(function(err, collection) {
    collection.ensureIndex({
        'name': 1,
        'name': 'text'
    }, function(err, result) {
        //nothing
    });
});

but this creates just the text index.

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

1 Answers1

1

To enable unique index in node you need to do :

City.native(function(err, collection) {
    collection.ensureIndex(
        {'name': 1},
        {unique:true}, 
        function(err, result) {
        //nothing
    });
});

Now to merge this code:(If no-ordering is specified for index then it is ascending order)

City.native(function(err, collection) {
    collection.ensureIndex(
        {'name': 'text'},
        {unique:true},
        function(err, result) {
        //nothing
    });
});
martskins
  • 2,920
  • 4
  • 26
  • 52
vmr
  • 1,895
  • 13
  • 24