Computer >> Máy Tính >  >> Lập trình >> MongoDB

Trong MongoDB sẽ giới hạn () tăng tốc độ truy vấn?

Không, việc sử dụng LIMIT () làm giảm mức tiêu thụ băng thông và không làm tăng tốc độ truy vấn. Hãy để chúng tôi xem một ví dụ và tạo một bộ sưu tập với các tài liệu -

> 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")
}

Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -

> db.demo197.find();

Điều này sẽ tạo ra kết quả sau -

{ "_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" }

Sau đây là truy vấn sử dụng LIMIT () -

> db.demo197.find().limit(4);

Điều này sẽ tạo ra kết quả sau -

{ "_id" : ObjectId("5e3afde803d395bdc21346d8"), "Name" : "Chris" }
{ "_id" : ObjectId("5e3afdef03d395bdc21346d9"), "Name" : "Bob" }
{ "_id" : ObjectId("5e3afdf203d395bdc21346da"), "Name" : "David" }
{ "_id" : ObjectId("5e3afdf603d395bdc21346db"), "Name" : "Sam" }