AND trong MongoDB

Cú pháp

Trong phương thức find(), nếu bạn truyền nhiều key bằng cách phân biệt chúng bởi dấu phảy (,), thì MongoDB xem nó như là điều kiện AND. Cú pháp cơ bản của AND trong MongoDB như sau:

>db.mycol.find({key1:value1, key2:value2}).pretty()

Ví dụ

Ví dụ sau hiển thị tất cả loạt bài hướng dẫn (tutorials) được viết bởi 'tutorials point' có title là 'MongoDB Overview'

>db.mycol.find({"by":"tutorials point","title": "MongoDB Overview"}).pretty()
{

"_id": ObjectId(7df78ad8902c),

"title": "MongoDB Overview",

"description": "MongoDB is no sql database",

"by": "tutorials point",

"url": "http://www.tutorialspoint.com",

"tags": ["mongodb", "database", "NoSQL"],

"likes": "100"

}

>

Mệnh đề WHERE tương đương với ví dụ trên sẽ là ' where by='tutorials point' AND title='MongoDB Overview' '. Bạn có thể truyền bất kỳ số cặp key-value nào trong mệnh đề find.

Last updated