1.db.users.ensureindex is not a function
> db.users.ensureindex({"Name":1,"Age":1})
2024-06-13T12:51:08.512+0800 E QUERY [js] TypeError: db.users.ensureindex is not a function :
@(shell):1:1
>
>
>
> db.users.ensureIndex({"Name":1,"Age":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
创建索引的关键字必须写为:ensureIndex,而不能写成 ensureindex;
可以根据具体的情况给集合创建索引和删除索引,创建索引后如何关系数据库的表,能够让执行计划都索引扫描,而没创建索引,则默认走全表扫描。
2.检查现有集合的索引
> db.users.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 2,
"key" : {
"Name" : 1
},
"name" : "Name_1",
"ns" : "test.users"
},
{
"v" : 2,
"key" : {
"Name" : 1,
"Age" : 1
},
"name" : "Name_1_Age_1",
"ns" : "test.users"
}
]
>
3.删除特定名称的索引
> db.users.dropIndex("Name_1")
{ "nIndexesWas" : 3, "ok" : 1 }
> db.users.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 2,
"key" : {
"Name" : 1,
"Age" : 1
},
"name" : "Name_1_Age_1",
"ns" : "test.users"
}
]
可以看到Name_1 这个索引已经被删除了。
4.删除所有的索引
> db.users.dropIndexes()
{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
>
>
> db.users.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
}
]
由此可以见:dropIndexes() 只能删除用户自定的索引,不能删除自带的索引 _id_;
5.创建唯一索引
> db.users.ensureIndex({"Name":1},{"unique":true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 2,
"unique" : true,
"key" : {
"Name" : 1
},
"name" : "Name_1",
"ns" : "test.users"
}
]
{"unique":true} 通过这个关键词指明是一个唯一索引。
6.多键值唯一索引
> db.users.ensureIndex({"Name":1,"Age":1},{"unique":true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 2,
"unique" : true,
"key" : {
"Name" : 1
},
"name" : "Name_1",
"ns" : "test.users"
},
{
"v" : 2,
"unique" : true,
"key" : {
"Name" : 1,
"Age" : 1
},
"name" : "Name_1_Age_1",
"ns" : "test.users"
}
]
7.有重复值的情况下创建唯一索引。
> db.users.createIndex({"Name":1,"Age":1},{"unique":true,"dropDups":true})
{
"ok" : 0,
"errmsg" : "E11000 duplicate key error collection: test.users index: Name_1_Age_1 dup key: { : \"Test User19\", : 29.0 }",
"code" : 11000,
"codeName" : "DuplicateKey"
}
dropDups是MongoDB中一个特殊的选项,用于在创建索引时防止集合中出现重复的值。
然而,从MongoDB 3.0+版本开始,dropDups选项已经被废弃,
并且在创建唯一索引时应使用unique选项来替代。
当前版本mongodb4.0.2,不支持 dropDups选项。
8.根据ID删除特定的记录
> db.users.find()
{ "_id" : ObjectId("666a6175a0184f8213fade86"), "FName" : "Test User", "Age" : 45, "Gender" : "F", "Country" : "US" }
{ "_id" : ObjectId("666a617da0184f8213fade87"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade88"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade89"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8a"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8b"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8c"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8d"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8e"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8f"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade90"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade91"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade92"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade93"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade94"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade95"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade96"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade97"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade98"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade99"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
Type "it" for more
> it
{ "_id" : ObjectId("666a617da0184f8213fade9a"), "Name" : "Test User20", "Age" : 30, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a7eeba0184f8213fade9c"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
> db.users.remove({"_id" : ObjectId("666a7eeba0184f8213fade9c")})
WriteResult({ "nRemoved" : 1 })
>
> db.users.find()
{ "_id" : ObjectId("666a6175a0184f8213fade86"), "FName" : "Test User", "Age" : 45, "Gender" : "F", "Country" : "US" }
{ "_id" : ObjectId("666a617da0184f8213fade87"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade88"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade89"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8a"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8b"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8c"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8d"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8e"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8f"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade90"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade91"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade92"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade93"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade94"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade95"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade96"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade97"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade98"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade99"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
Type "it" for more
> it
{ "_id" : ObjectId("666a617da0184f8213fade9a"), "Name" : "Test User20", "Age" : 30, "Gender" : "F", "Country" : "India" }
>
9.重新创建唯一索引
>
> db.users.createIndex({"Name":1,"Age":1},{"unique":true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 2,
"unique" : true,
"key" : {
"Name" : 1,
"Age" : 1
},
"name" : "Name_1_Age_1",
"ns" : "test.users"
}
]