Computer >> Máy Tính >  >> Lập trình >> MongoDB

Truy vấn trong MongoDB để thực hiện một thao tác tương tự như thao tác LIKE

Đối với thao tác tương tự, bạn có thể sử dụng / searchLetter /. Trước tiên, hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -

> db.demo26.insertOne ({"StudentName":"Chris"}); {"inherit":true, "insertId":ObjectId ("5e14c9dc22d07d3b95082e79")}> db.demo26.insertOne ({"StudentName" :"John"}); {"Recognition":true, "insertId":ObjectId ("5e14c9e022d07d3b95082e7a")}> db.demo26.insertOne ({"StudentName":"Jones"}); {"Recognition":true, "insertId":ObjectId ("5e14ca7222d07d3b95082e7b")}> db.demo26.insertOne ({"StudentName":"David"}); {"nition ":true," insertId ":ObjectId (" 5e14ca7622d07d3b95082e7c "> 

Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -

> db.demo26.find (); 

Điều này sẽ tạo ra kết quả sau -

 {"_id":ObjectId ("5e14c9dc22d07d3b95082e79"), "StudentName":"Chris"} {"_id":ObjectId ("5e14c9e022d07d3b95082e7a"), "StudentName":"John"} {"_id":ObjectId ( "5e14ca7222d07d3b95082e7b"), "StudentName":"Jones"} {"_id":ObjectId ("5e14ca7622d07d3b95082e7c"), "StudentName":"David"} 

Sau đây là truy vấn sử dụng câu lệnh “LIKE” trong MongoDB -

> db.demo26.find ({"StudentName":/ Jo /}); 

Điều này sẽ tạo ra kết quả sau -

 {"_id":ObjectId ("5e14c9e022d07d3b95082e7a"), "StudentName":"John"} {"_id":ObjectId ("5e14ca7222d07d3b95082e7b"), "StudentName":"Jones"}