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

Biểu thức chính quy MongoDB để khớp với một bản ghi cụ thể?

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 -

> {"StudentDetails":{"StudentName":"JOHN"}, "StudentAge":19}); {"nition ":true," insertId ":ObjectId (" 5cf227b8b64a577be5a2bc08 ")}> dbworkingOfRegularExpressionDemoinsertOneails ({" StudentDetails " "StudentName":"Carol"}, "StudentAge":20}); {"nition ":true," insertId ":ObjectId (" 5cf227c2b64a577be5a2bc09 ")}

Sau đây là truy vấn để 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 () -

> dbworkingOfRegularExpressionDemofind (); 

Điều này sẽ tạo ra tài liệu sau -

 {"_id":ObjectId ("5cf227acb64a577be5a2bc07"), "StudentDetails":{"StudentName":"John"}, "StudentAge":21} {"_id":ObjectId ("5cf227b8bails64a577be5a2bc08") :{"StudentName":"JOHN"}, "StudentAge":19} {"_id":ObjectId ("5cf227c2b64a577be5a2bc09"), "StudentDetails":{"StudentName":"Carol"}, "StudentAge":20} Đang theo dõi là biểu thức chính quy để lấy tài liệu với StudentName JOHN:> dbworkingOfRegularExpressionDemofind ({'StudentDetailsStudentName':/ JOHN /}); 

Điều này sẽ tạo ra tài liệu sau -

 {"_id":ObjectId ("5cf227b8b64a577be5a2bc08"), "StudentDetails":{"StudentName":"JOHN"}, "StudentAge":19}