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

Truy vấn MongoDB với điều kiện OR

Để thực hiện truy vấn với điều kiện OR (hoặc) trong MongoDB, bạn sử dụng toán tử $or.

Cú pháp

db.collection.find({ $or: [ {condition1}, {condition2}, ... ] })

Ví dụ minh họa

Tạo collection và dữ liệu

> db.orConditionDemo.insertOne({"CustomerName":"Larry","ShippingDate":new ISODate("2018-01-29")});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ec5262f684a30fbdfd56a") }
> db.orConditionDemo.insertOne({"CustomerName":"Mike","ShippingDate":new ISODate("2019-04-13")});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ec5362f684a30fbdfd56b") }
> db.orConditionDemo.insertOne({"CustomerName":"Bob","ShippingDate":new ISODate("2019-02-21")});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ec5422f684a30fbdfd56c") }
> db.orConditionDemo.insertOne({"CustomerName":"David","ShippingDate":new ISODate("2019-03-15")});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ec5532f684a30fbdfd56d") }
> db.orConditionDemo.insertOne({"CustomerName":"John","ShippingDate":new ISODate("2019-03-19")});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ec56c2f684a30fbdfd56e") }

Xem tất cả documents

> db.orConditionDemo.find().pretty();
{
    "_id" : ObjectId("5c8ec5262f684a30fbdfd56a"),
    "CustomerName" : "Larry",
    "ShippingDate" : ISODate("2018-01-29T00:00:00Z")
}
{
    "_id" : ObjectId("5c8ec5362f684a30fbdfd56b"),
    "CustomerName" : "Mike",
    "ShippingDate" : ISODate("2019-04-13T00:00:00Z")
}
{
    "_id" : ObjectId("5c8ec5422f684a30fbdfd56c"),
    "CustomerName" : "Bob",
    "ShippingDate" : ISODate("2019-02-21T00:00:00Z")
}
{
    "_id" : ObjectId("5c8ec5532f684a30fbdfd56d"),
    "CustomerName" : "David",
    "ShippingDate" : ISODate("2019-03-15T00:00:00Z")
}
{
    "_id" : ObjectId("5c8ec56c2f684a30fbdfd56e"),
    "CustomerName" : "John",
    "ShippingDate" : ISODate("2019-03-19T00:00:00Z")
}

Truy vấn với nhiều điều kiện OR

Tìm order có ShippingDate >= hôm nay HOẶC ShippingDate = null:

> db.orConditionDemo.find({$or: [
    {ShippingDate: {$gte: new ISODate()}}, 
    {ShippingDate: null}
]}).pretty();
{
    "_id" : ObjectId("5c8ec5362f684a30fbdfd56b"),
    "CustomerName" : "Mike",
    "ShippingDate" : ISODate("2019-04-13T00:00:00Z")
}
{
    "_id" : ObjectId("5c8ec56c2f684a30fbdfd56e"),
    "CustomerName" : "John",
    "ShippingDate" : ISODate("2019-03-19T00:00:00Z")
}

Giả sử ngày hiện tại là 2019-03-15, thì Mike (2019-04-13) và John (2019-03-19) thỏa mãn ShippingDate >= hôm nay. Không document nào có ShippingDate = null.

Các pattern OR phổ biến

Mục đíchCú pháp
Trường A = X HOẶC Trường B = Y{$or: [{A: X}, {B: Y}]}
Trường A trong [1,2] HOẶC Trường B > 10{$or: [{A: {$in: [1,2]}}, {B: {$gt: 10}}]}
Trường tồn tại HOẶC trường khác null{$or: [{field1: {$exists: true}}, {field2: {$ne: null}}]}
Regex match A HOẶC regex match B{$or: [{name: /pattern1/}, {name: /pattern2/}]}

Kết hợp $or với $and

// (A=1 OR B=2) AND C=3
db.coll.find({
    $and: [
        { $or: [{A: 1}, {B: 2}] },
        { C: 3 }
    ]
});

// Tương đương (implicit AND)
db.coll.find({
    $or: [{A: 1}, {B: 2}],
    C: 3
});

Lưu ý hiệu năng

  • Mỗi điều kiện trong $or nên có index riêng để tối ưu
  • MongoDB 2.6+ có thể dùng index intersection cho $or
  • Tránh $or với quá nhiều điều kiện (hiệu năng giảm)
  • Cân nhắc restructure schema để tránh $or phức tạp