2014-09-01 76 views
0

多个指标要启用节点唯一索引我做的:MongoDB的每场

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

但我想启用名称文本索引也。所以在做了以上我之后:

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

这完美地创造了两个指数。我的问题是,有没有机会合并此代码?我尝试过

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

但这只是创建文本索引。

+1

你(如果指数则上升顺序没有指定排序):

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

现在合并这段代码想要创建两个索引。这需要运行两个创建索引命令。 – 2014-09-01 20:02:22

回答

1

要启用节点唯一索引,你需要做的:

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

这不应该是'.ensureIndex({name:'text'},{unique:true},function(err,result){... nothing ...})? – lascort 2014-09-01 19:09:36

+0

是@lascort ...编辑帖子,我错过了大括号,谢谢。 – vmr 2014-09-01 19:15:03

+0

现在您的ensureIndex是“过卷曲支撑”:P – lascort 2014-09-01 19:26:06