将唯一索引更改为MongoDB中的稀疏唯一索引?
对于稀疏索引,请使用sparse:true。以下是创建索引的查询-
> db.demo229.ensureIndex({"ClientName":1}, {unique: true});
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}以下是显示索引的查询-
> db.demo229.getIndexes();
这将产生以下输出-
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.demo229"
},
{
"v" : 2,
"unique" : true,
"key" : {
"ClientName" : 1
},
"name" : "ClientName_1",
"ns" : "test.demo229"
}
]现在让我们删除一个索引,并将唯一索引更改为MongoDB中的稀疏唯一索引-
> db.demo229.dropIndex("ClientName_1");
{ "nIndexesWas" : 2, "ok" : 1 }
> db.demo229.ensureIndex({"ClientName":1}, {unique: true, sparse:true});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}以下是显示索引的查询-
> db.demo229.getIndexes();
这将产生以下输出-
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.demo229"
},
{
"v" : 2,
"unique" : true,
"key" : {
"ClientName" : 1
},
"name" : "ClientName_1",
"ns" : "test.demo229",
"sparse" : true
}
]热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短