Truy vấn Document trong MongoDB

Phương thức find() trong MongoDB

Để truy vấn dữ liệu từ Collection trong MongoDB, bạn cần sử dụng phương thức find() trong MongoDB.

Cú pháp

Cú pháp cơ bản của phương thức find() là như sau:

>db.COLLECTION_NAME.find()

Phương thức find() sẽ hiển thị tất cả Document ở dạng không có cấu trúc (hiển thị không theo cấu trúc nào).

Phương thức pretty() trong MongoDB

Để hiển thị các kết quả theo một cách đã được định dạng, bạn có thể sử dụng phương thức pretty().

Cú pháp

>db.mycol.find().pretty()

Ví dụ

>db.mycol.find().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"
}
>

Ngoài phương thức find(), trong MongoDB còn có phương thức findOne() sẽ chỉ trả về một Document.

Truy vấn trong MongoDB mà tương đương mệnh đề WHERE trong RDBMS

Để truy vấn Document dựa trên một số điều kiện nào đó, bạn có thể sử dụng các phép toán sau:

Phép toánCú phápVí dụMệnh đề WHERE tương đương
Equality{<key>:<value>}db.mycol.find({"by":"tutorials point"}).pretty()where by = 'tutorials point'
Less Than{<key>:{$lt:<value>}}db.mycol.find({"likes":{$lt:50}}).pretty()where likes < 50
Less Than Equals{<key>:{$lte:<value>}}db.mycol.find({"likes":{$lte:50}}).pretty()where likes <= 50
Greater Than{<key>:{$gt:<value>}}db.mycol.find({"likes":{$gt:50}}).pretty()where likes > 50
Greater Than Equals{<key>:{$gte:<value>}}db.mycol.find({"likes":{$gte:50}}).pretty()where likes >= 50
Not Equals{<key>:{$ne:<value>}}db.mycol.find({"likes":{$ne:50}}).pretty()where likes != 50

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.

OR trong MongoDB

Cú pháp

Để truy vấn Document dựa trên điều kiện OR, bạn cần sử dụng từ khóa $or. Cú pháp cơ bản của OR trong MongoDB như sau:

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

Ví dụ

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

>db.mycol.find({$or:[{"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"
}
>

Sử dụng AND và OR cùng nhau trong MongoDB

Ví dụ

Ví dụ sau hiển thị các Document mà có các like lớn hơn 100 và có title là hoặc 'MongoDB Overview' hoặc bởi là 'tutorials point'. Mệnh đề WHERE trong truy vấn SQL tương đương là 'where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')'

>db.mycol.find({"likes": {$gt:10}, $or: [{"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"
}
>

Theo Tutorialspoint

Bài trước: Chèn Document trong MongoDB

Bài tiếp: Cập nhật Document trong MongoDB

Thứ Năm, 23/08/2018 15:46
52 👨 1.016
0 Bình luận
Sắp xếp theo
    ❖ Học MongoDB