在MongoDB中limit()会提高查询速度吗?
不可以,使用LIMIT()可以减少带宽消耗,但不会提高查询速度。让我们看一个示例并创建包含文档的集合-
> db.demo197.insertOne({"Name":"Chris"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e3afde803d395bdc21346d8")
}
> db.demo197.insertOne({"Name":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3afdef03d395bdc21346d9")
}
> db.demo197.insertOne({"Name":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3afdf203d395bdc21346da")
}
> db.demo197.insertOne({"Name":"Sam"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3afdf603d395bdc21346db")
}
> db.demo197.insertOne({"Name":"Mike"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3afdf903d395bdc21346dc")
}
> db.demo197.insertOne({"Name":"Carol"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3afe1603d395bdc21346dd")
}
> db.demo197.insertOne({"Name":"John"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3afe2003d395bdc21346de")
}
在find()方法的帮助下显示集合中的所有文档-
> db.demo197.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3afde803d395bdc21346d8"), "Name" : "Chris" }{ "_id" : ObjectId("5e3afdef03d395bdc21346d9"), "Name" : "Bob" }
{ "_id" : ObjectId("5e3afdf203d395bdc21346da"), "Name" : "David" }
{ "_id" : ObjectId("5e3afdf603d395bdc21346db"), "Name" : "Sam" }
{ "_id" : ObjectId("5e3afdf903d395bdc21346dc"), "Name" : "Mike" }
{ "_id" : ObjectId("5e3afe1603d395bdc21346dd"), "Name" : "Carol" }
{ "_id" : ObjectId("5e3afe2003d395bdc21346de"), "Name" : "John" }
以下是使用LIMIT()的查询-
> db.demo197.find().limit(4);
这将产生以下输出-
{ "_id" : ObjectId("5e3afde803d395bdc21346d8"), "Name" : "Chris" }{ "_id" : ObjectId("5e3afdef03d395bdc21346d9"), "Name" : "Bob" }
{ "_id" : ObjectId("5e3afdf203d395bdc21346da"), "Name" : "David" }
{ "_id" : ObjectId("5e3afdf603d395bdc21346db"), "Name" : "Sam" }
以上是 在MongoDB中limit()会提高查询速度吗? 的全部内容, 来源链接: utcz.com/z/349054.html