为什么MongoDB花费太多时间来查找记录?
在这种情况下,请在特定字段上使用索引的概念。首先让我们创建一个包含文档的集合。在这里,我们还使用createIndex()-创建了索引
> db.decreasetimeusingindex.createIndex({"StudentName":1});
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.decreasetimeusingindex.insertOne({"StudentName":"John Smith","StudentAge":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e03960af5e889d7a51994ff")
}
> db.decreasetimeusingindex.insertOne({"StudentName":"John Doe","StudentAge":24});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e039611f5e889d7a5199500")
}
> db.decreasetimeusingindex.insertOne({"StudentName":"Adam Smith","StudentAge":22});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e03961df5e889d7a5199501")
}以下是在find()方法的帮助下显示集合中所有文档的查询-
> db.decreasetimeusingindex.find().pretty();
这将产生以下输出-
{
"_id" : ObjectId("5e03960af5e889d7a51994ff"),
"StudentName" : "John Smith",
"StudentAge" : 21
}
{
"_id" : ObjectId("5e039611f5e889d7a5199500"),
"StudentName" : "John Doe",
"StudentAge" : 24
}
{
"_id" : ObjectId("5e03961df5e889d7a5199501"),
"StudentName" : "Adam Smith",
"StudentAge" : 22
}以下是查找特定记录的查询-
> db.decreasetimeusingindex.find({"StudentName":"Adam Smith"});这将产生以下输出-
{ "_id" : ObjectId("5e03961df5e889d7a5199501"), "StudentName" : "Adam Smith", "StudentAge" : 22 }热门推荐
10 新人给朋友祝福语简短
11 男孩上学祝福语简短英语
12 三八简短祝福语给婆婆
13 创业失败返乡祝福语简短
14 男朋友祝福语简短新年
15 新房开工吉日祝福语简短
16 给姐妹乔迁祝福语简短
17 写给情侣祝福语 浪漫简短
18 舅妈生日红包祝福语简短