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
}
] -
MongoDBのコレクションに一意のインデックスであるフィールドを追加しますか?
一意のインデックスの場合、インデックスの作成時にunique-trueを設定します。ドキュメントを使用してコレクションを作成しましょう- > db.demo658.createIndex({FirstName:1},{unique:true,sparse:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, &n
-
MongoDBコレクションにインデックスを作成しますか?
インデックスを作成するには、MongoDBでcreateIndex()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo702.createIndex({"details.id":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, &q