Để truy vấn một chuỗi mảng cho đối sánh regexp, hãy sử dụng cú pháp sau
db.yourCollectionName.find( { yourFieldName: /yourStartingValue./ } ).pretty(); 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.queryArrayDemo.insertOne({"StudentFullName":["Carol Taylor","Caroline
Williams","Claire Brown"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca2774c6304881c5ce84ba0")
}
> db.queryArrayDemo.insertOne({"StudentFullName":["John Smith","Jace Doe","Jabin
Brown"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca277b36304881c5ce84ba1")
} 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 ()
> db.queryArrayDemo.find().pretty();
Điều này sẽ tạo ra kết quả sau
{
"_id" : ObjectId("5ca2774c6304881c5ce84ba0"),
"StudentFullName" : [
"Carol Taylor",
"Caroline Williams",
"Claire Brown"
]
}
{
"_id" : ObjectId("5ca277b36304881c5ce84ba1"),
"StudentFullName" : [
"John Smith",
"Jace Doe",
"Jabin Brown"
]
} Đây là cách bạn có thể truy vấn một chuỗi mảng để có kết quả khớp regexp
> db.queryArrayDemo.find( { StudentFullName : /J./ } ).pretty(); Điều này sẽ tạo ra kết quả sau
{
"_id" : ObjectId("5ca277b36304881c5ce84ba1"),
"StudentFullName" : [
"John Smith",
"Jace Doe",
"Jabin Brown"
]
}