要在节点I中启用唯一索引,请执行以下操作:
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
});
});
但这只会创建文本索引。
答案 0 :(得分:1)
要在节点中启用唯一索引,您需要执行以下操作:
City.native(function(err, collection) {
collection.ensureIndex(
{'name': 1},
{unique:true},
function(err, result) {
//nothing
});
});
现在合并此代码:(如果为索引指定了无序,则它是升序)
City.native(function(err, collection) {
collection.ensureIndex(
{'name': 'text'},
{unique:true},
function(err, result) {
//nothing
});
});