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

MongoDB Aggregation Framework: Sử dụng $match với điều kiện OR

Trong MongoDB Aggregation Framework, bạn có thể sử dụng toán tử $or bên trong stage $match để lọc document theo nhiều điều kiện (OR logic).

Ví dụ minh họa

Tạo collection và dữ liệu

> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith","StudentAge":23});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ac3a96cea1f28b7aa080d") }
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Tayor","StudentAge":24});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ac3bc6cea1f28b7aa080e") }
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller","StudentAge":21});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ac3ce6cea1f28b7aa080f") }
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Taylor","StudentAge":20});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ac3e16cea1f28b7aa0810") }
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Robert", "StudentLastName":"Smith","StudentAge":20});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ac3fb6cea1f28b7aa0811") }
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Mike", "StudentLastName":"Miller","StudentAge":27});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ac4166cea1f28b7aa0812") }

Xem tất cả documents

> db.aggregationFrameworkWithOrMatchDemo.find().pretty();
{
    "_id" : ObjectId("5c8ac3a96cea1f28b7aa080d"),
    "StudentFirstName" : "John",
    "StudentLastName" : "Smith",
    "StudentAge" : 23
}
{
    "_id" : ObjectId("5c8ac3bc6cea1f28b7aa080e"),
    "StudentFirstName" : "Carol",
    "StudentLastName" : "Tayor",
    "StudentAge" : 24
}
{
    "_id" : ObjectId("5c8ac3ce6cea1f28b7aa080f"),
    "StudentFirstName" : "David",
    "StudentLastName" : "Miller",
    "StudentAge" : 21
}
{
    "_id" : ObjectId("5c8ac3e16cea1f28b7aa0810"),
    "StudentFirstName" : "Bob",
    "StudentLastName" : "Taylor",
    "StudentAge" : 20
}
{
    "_id" : ObjectId("5c8ac3fb6cea1f28b7aa0811"),
    "StudentFirstName" : "Robert",
    "StudentLastName" : "Smith",
    "StudentAge" : 20
}
{
    "_id" : ObjectId("5c8ac4166cea1f28b7aa0812"),
    "StudentFirstName" : "Mike",
    "StudentLastName" : "Miller",
    "StudentAge" : 27
}

Aggregation với $match và $or

Tìm các student có họ là Smith HOẶC Miller:

> db.aggregationFrameworkWithOrMatchDemo.aggregate([
    { $match: { $or: [ { StudentLastName: 'Smith' }, { StudentLastName: 'Miller' } ] } }
]).pretty();
{
    "_id" : ObjectId("5c8ac3a96cea1f28b7aa080d"),
    "StudentFirstName" : "John",
    "StudentLastName" : "Smith",
    "StudentAge" : 23
}
{
    "_id" : ObjectId("5c8ac3ce6cea1f28b7aa080f"),
    "StudentFirstName" : "David",
    "StudentLastName" : "Miller",
    "StudentAge" : 21
}
{
    "_id" : ObjectId("5c8ac3fb6cea1f28b7aa0811"),
    "StudentFirstName" : "Robert",
    "StudentLastName" : "Smith",
    "StudentAge" : 20
}
{
    "_id" : ObjectId("5c8ac4166cea1f28b7aa0812"),
    "StudentFirstName" : "Mike",
    "StudentLastName" : "Miller",
    "StudentAge" : 27
}

Cấu trúc pipeline

db.collection.aggregate([
    { $match: { $or: [ { field1: value1 }, { field2: value2 } ] } },
    { $group: { ... } },
    { $sort: { ... } },
    // ... các stage khác
])

Lưu ý hiệu năng

  • Đặt $match với $or ở đầu pipeline để lọc sớm, giảm dữ liệu xử lý
  • Đảm bảo các trường trong $or có index để tối ưu hiệu năng
  • $or trong aggregation hoạt động giống hệt $or trong find()