Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Cách lấy giá trị lớn nhất của một trường trong MongoDB

Để lấy giá trị lớn nhất (maximum) của một trường trong MongoDB, bạn sử dụng sort() giảm dần kết hợp limit(1).

Cú pháp

db.yourCollectionName.find().sort({"yourFieldName": -1}).limit(1);

Ví dụ minh họa

Tạo collection và dữ liệu

> db.gettingHighestValueDemo.insertOne({"Value":1029});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900b885705caea966c5574") }
> db.gettingHighestValueDemo.insertOne({"Value":3029});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900b8d5705caea966c5575") }
> db.gettingHighestValueDemo.insertOne({"Value":1092});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900b925705caea966c5576") }
> db.gettingHighestValueDemo.insertOne({"Value":18484});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900b955705caea966c5577") }
> db.gettingHighestValueDemo.insertOne({"Value":37474});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900b9c5705caea966c5578") }
> db.gettingHighestValueDemo.insertOne({"Value":88474});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900ba75705caea966c5579") }
> db.gettingHighestValueDemo.insertOne({"Value":1938474});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900bab5705caea966c557a") }

Xem tất cả documents

> db.gettingHighestValueDemo.find().pretty();
{
    "_id" : ObjectId("5c900b885705caea966c5574"),
    "Value" : 1029
}
{
    "_id" : ObjectId("5c900b8d5705caea966c5575"),
    "Value" : 3029
}
{
    "_id" : ObjectId("5c900b925705caea966c5576"),
    "Value" : 1092
}
{
    "_id" : ObjectId("5c900b955705caea966c5577"),
    "Value" : 18484
}
{
    "_id" : ObjectId("5c900b9c5705caea966c5578"),
    "Value" : 37474
}
{
    "_id" : ObjectId("5c900ba75705caea966c5579"),
    "Value" : 88474
}
{
    "_id" : ObjectId("5c900bab5705caea966c557a"),
    "Value" : 1938474
}

Lấy document có Value lớn nhất

> db.gettingHighestValueDemo.find().sort({"Value":-1}).limit(1);
{
    "_id" : ObjectId("5c900bab5705caea966c557a"),
    "Value" : 1938474
}

Các cách khác lấy giá trị max

1. Aggregation $max (chỉ lấy giá trị, không lấy document)

> db.gettingHighestValueDemo.aggregate([
    { $group: { _id: null, maxValue: { $max: "$Value" } } }
]);
{ "_id" : null, "maxValue" : 1938474 }

2. Aggregation lấy toàn bộ document có max

db.gettingHighestValueDemo.aggregate([
    { $sort: { Value: -1 } },
    { $limit: 1 }
])

3. findOne với sort (trả về object trực tiếp)

db.gettingHighestValueDemo.findOne({}, {sort: {Value: -1}});

So sánh các phương pháp

Phương phápTrả vềHiệu năngLưu ý
find().sort().limit(1)Cursor (1 doc)Tốt (có index)Chuẩn, linh hoạt
findOne({}, {sort: ...})DocumentTốtNgắn gọn
aggregate([$max])Giá trị maxTốtChỉ lấy giá trị, không doc
aggregate([$sort, $limit])Cursor (1 doc)TốtDùng trong pipeline

Lưu ý quan trọng

  • Tạo index trên trường cần sort để tránh full collection scan: db.coll.createIndex({Value: -1})
  • Nếu trường có giá trị null, sort sẽ đặt null ở đầu (giá trị nhỏ nhất)
  • Dùng {$exists: true} trong query để loại trừ null nếu cần