db.teams.findOne[{team: {$regex : /avs/}}]
Truy vấn này trả về tài liệu sau
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }
Lưu ý rằng hàm findOne[] trả về tài liệu đầu tiên trong bộ sưu tập thỏa mãn tiêu chí truy vấn
Điều này có nghĩa là các nhóm khác cũng có thể có chuỗi 'avs' trong tên nhóm của họ, nhưng tài liệu chứa tên nhóm 'Mavs' chỉ đơn giản là tài liệu đầu tiên
ví dụ 2. Kiểm tra xem Trường có chứa chuỗi [Không phân biệt chữ hoa chữ thường]
Chúng ta cũng có thể sử dụng i sau chuỗi để thực hiện so khớp không phân biệt chữ hoa chữ thường.
Ví dụ: giả sử chúng tôi sử dụng truy vấn sau
db.teams.findOne[{team: {$regex : /AVS/i}}]
Truy vấn này cũng trả về tài liệu sau
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }
ví dụ 3. Kiểm tra xem trường có chứa chuỗi không [Không có kết quả]
Nếu một trường không chứa chuỗi cụ thể mà chúng tôi đã tìm kiếm, kết quả là chúng tôi sẽ chỉ nhận được null
MongoDB tìm cú pháp khá đơn giản. Hãy có một bộ sưu tập tên là
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
6 và tìm tất cả các tài liệu trong bộ sưu tập,Nếu chúng ta cần lọc theo một số trường thì,
Điều gì sẽ xảy ra nếu chúng tôi cần lọc theo trường nhưng chúng tôi không biết trường hợp chính xác hoặc giá trị chính xác. Hãy xem ví dụ
- liệt kê tất cả các tài liệu có từ
7 trong trường>db.restaurants.find[ { cuisine: "Hamburgers" } ] { "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" } ...
8>db.restaurants.find[ { cuisine: "Hamburgers" } ] { "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" } ...
Lọc tài liệu bằng biểu thức chính quy
Nó có thể dễ dàng đạt được bằng cách sử dụng biểu thức chính quy thay vì giá trị chuỗi,
Đây
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
9 là biểu thức chính quy sẽ tìm tất cả các từ khớp với >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
7. Nhưng truy vấn này phân biệt chữ hoa chữ thường. Nó sẽ không khớp với >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
1. Nhưng rất dễ viết truy vấn không phân biệt chữ hoa chữ thường,Thêm
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
2 vào cuối biểu thị regex khớp với từ bất kể trường hợp nào. [chữ hoa, chữ thường, v.v.]Chúng tôi có thể sử dụng bất kỳ tìm kiếm dựa trên regex nào để lọc các giá trị trong mongoDB. Chúng ta sẽ sớm tìm hiểu thêm các thủ thuật regex và mongoDB
MongoDB rất mạnh và nó cung cấp rất nhiều phương thức để truy vấn chính xác những gì bạn muốn. Hy vọng bạn thấy hướng dẫn này hữu ích 🤗
Đối với hướng dẫn này, chúng tôi sẽ sử dụng
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
3, chứa nhiều tài liệu về nhà hàng từ khắp khu vực New YorkDưới đây là một ví dụ về cấu trúc tài liệu cơ bản trong bộ sưu tập này, sử dụng phương pháp
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
4> db.restaurants.findOne[]
{
"_id" : ObjectId["56c651e7d84ccfde319961af"],
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate["2014-12-30T00:00:00Z"],
"grade" : "A",
"score" : 8
},
{
"date" : ISODate["2014-07-01T00:00:00Z"],
"grade" : "B",
"score" : 23
},
{
"date" : ISODate["2013-04-30T00:00:00Z"],
"grade" : "A",
"score" : 12
},
{
"date" : ISODate["2012-05-08T00:00:00Z"],
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
Sức mạnh của Tìm kiếm
Phần quan trọng nhất của câu đố khi tìm kiếm trong bộ sưu tập MongoDB là phương pháp
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
0 đơn giản nhưng linh hoạtVới
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
1, bạn có thể dễ dàng truy vấn một tập hợp các tài liệu bằng cách chuyển một vài tham số đơn giản và trả về một >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
2. Một >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
2 chỉ đơn giản là một tập hợp kết quả và có thể được lặp lại để thao tác hoặc sử dụng các tài liệu được chỉ ra bởi >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
2Như một ví dụ đơn giản về thực tế của phương pháp
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
1, chúng tôi sẽ cố gắng tìm tất cả các nhà hàng trong bộ sưu tập của chúng tôi mà máy chủ >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
6 là >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
7 của họ>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
Tập hợp kết quả khá lớn, do đó, phép đo tốt hơn cho các ví dụ thử nghiệm của chúng tôi sẽ là xâu chuỗi phương thức
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
8 vào >db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
1 để chỉ cần xem có bao nhiêu tài liệu phù hợp với truy vấn của chúng tôi{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }6
Đó là rất nhiều bánh mì kẹp thịt
Tìm kiếm từ tương tự bằng cách sử dụng Regex
Bây giờ chúng tôi đang sử dụng
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
1 để truy vấn bộ sưu tập của mình, chúng tôi thực sự có thể sửa đổi cú pháp của mình một chút và bắt đầu tìm kiếm các kết quả khớp dựa trên một từ hoặc cụm từ có thể khớp một phần trong một trường nhất định, tương tự như toán tử { _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }61 cho các công cụ SQL
Mẹo nhỏ là sử dụng
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }62 [hoặc viết tắt là
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63], về cơ bản là một chuỗi văn bản xác định mẫu tìm kiếm. Có một số công cụ
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 được viết theo cú pháp hơi khác nhau, nhưng các nguyên tắc cơ bản đều giống nhau và trong trường hợp này, MongoDB sử dụng công cụ
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }65
Ở cấp độ cơ bản nhất, một biểu thức
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 là một chuỗi [chuỗi ký tự] được đặt ở cả hai bên bởi một dấu gạch chéo [
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }67]
Ví dụ: nếu chúng tôi muốn sử dụng
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 để thực hiện cùng một truy vấn như trên và tìm hiểu xem có bao nhiêu nhà hàng phục vụ
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
6, chúng tôi có thể thay thế chuỗi db.teams.findOne[{team: {$regex : /AVS/i}}]70 của mình bằng
db.teams.findOne[{team: {$regex : /AVS/i}}]71
db.teams.findOne[{team: {$regex : /AVS/i}}]7
Những người quan sát tinh ý có thể nhận ra rằng chúng tôi thực sự không thay đổi gì về truy vấn thực tế mà chúng tôi đang thực hiện – chúng tôi vẫn chỉ đơn giản là tra cứu tất cả các tài liệu trong đó trường
>db.restaurants.find[ { cuisine: "Hamburgers" } ]
{ "_id" : ObjectId["56c651e7d84ccfde319961af"], "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : ISODate["2014-12-30T00:00:00Z"], "grade" : "A", "score" : 8 }, { "date" : ISODate["2014-07-01T00:00:00Z"], "grade" : "B", "score" : 23 }, { "date" : ISODate["2013-04-30T00:00:00Z"], "grade" : "A", "score" : 12 }, { "date" : ISODate["2012-05-08T00:00:00Z"], "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
...
7 bằng với chuỗi db.teams.findOne[{team: {$regex : /AVS/i}}]70
Điều đó nói rằng, chỉ bằng cách sử dụng
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 thay vì một “chuỗi trích dẫn” thông thường, chúng ta có thể bắt đầu tìm kiếm các từ/cụm từ phù hợp một phần thay thế
Ví dụ: hãy xem trường
db.teams.findOne[{team: {$regex : /AVS/i}}]75 để hiểu rõ hơn về cách thức hoạt động của trường này. Trước tiên, chúng tôi sẽ nhận thấy rằng có tổng cộng sáu quận trong bộ sưu tập của chúng tôi
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }4
Bây giờ, hãy sử dụng
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 để tìm xem có bao nhiêu nhà hàng trong quận
db.teams.findOne[{team: {$regex : /AVS/i}}]77
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }7
Nhưng hãy tưởng tượng chúng ta muốn tìm số lượng nhà hàng mà
db.teams.findOne[{team: {$regex : /AVS/i}}]75 bắt đầu bằng ba ký tự đầu tiên
db.teams.findOne[{team: {$regex : /AVS/i}}]79. Chúng tôi sẽ sửa đổi
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 của mình một chút, như vậy
> db.restaurants.findOne[]
{
"_id" : ObjectId["56c651e7d84ccfde319961af"],
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate["2014-12-30T00:00:00Z"],
"grade" : "A",
"score" : 8
},
{
"date" : ISODate["2014-07-01T00:00:00Z"],
"grade" : "B",
"score" : 23
},
{
"date" : ISODate["2013-04-30T00:00:00Z"],
"grade" : "A",
"score" : 12
},
{
"date" : ISODate["2012-05-08T00:00:00Z"],
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
1Chúng tôi đang xem hơn 6000 tài liệu bổ sung trong tập hợp kết quả này, điều này có ý nghĩa bởi vì chúng tôi không chỉ nhận được kết quả trong đó
db.teams.findOne[{team: {$regex : /AVS/i}}]75 là
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }42 mà còn mọi thứ cho
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }43 nữa
Ký tự dấu mũ [
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }44] chỉ định vị trí trong chuỗi của chúng tôi. Vị trí này sẽ là phần đầu, vì vậy nếu chúng tôi có một tài liệu mà ba chữ cái đó nằm ở giữa trường, thì chúng tôi sẽ không khớp
Một ví dụ nhanh khác, hãy tìm kiếm bất kỳ nơi nào trong trường cho các ký tự
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }45, điều này sẽ cho chúng ta kết quả cho cả
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }46 và
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }47
> db.restaurants.findOne[]
{
"_id" : ObjectId["56c651e7d84ccfde319961af"],
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate["2014-12-30T00:00:00Z"],
"grade" : "A",
"score" : 8
},
{
"date" : ISODate["2014-07-01T00:00:00Z"],
"grade" : "B",
"score" : 23
},
{
"date" : ISODate["2013-04-30T00:00:00Z"],
"grade" : "A",
"score" : 12
},
{
"date" : ISODate["2012-05-08T00:00:00Z"],
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
9Chắc chắn rồi, truy vấn cuối cùng của chúng tôi đã kết hợp hai tập hợp kết quả thành một
Bạn có thể nhận thấy rằng mặc dù các ký tự
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }48 của chúng tôi là chữ hoa trong chuỗi
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 của chúng tôi, nhưng chúng là chữ thường trong các bản ghi tài liệu thực tế, chúng tôi vẫn trả về kết quả. Điều này là do chúng tôi cũng đã thêm cờ
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }70 đặc biệt sau dấu gạch chéo đóng biểu thức chính quy của chúng tôi [
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }67]. Điều này thông báo cho công cụ
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }63 rằng chúng tôi muốn tìm kiếm là
{ _id: ObjectId["618050098ffcfe76d07b1da5"], team: 'Mavs', position: 'Guard', points: 31 }73, khớp bất kể chữ hoa hay chữ thường