Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7Mới trong phiên bản 4.2.
Thực hiện khớp mẫu biểu thức thông thường [Regex] và trả về:
8 Nếu một trận đấu tồn tại.db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
9 Nếu một trận đấu không tồn tại.db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
MongoDB sử dụng các biểu thức thông thường tương thích Perl [tức là "PCRE"] phiên bản 8.41 với hỗ trợ UTF-8.
Trước MongoDB 4.2, đường ống tổng hợp chỉ có thể sử dụng toán tử truy vấn
db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
0 trong giai đoạn db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
1. Để biết thêm thông tin về việc sử dụng Regex trong truy vấn, xem db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
0Toán tử
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 có cú pháp sau:db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 operator has the following syntax:{ $regexMatch: { input: , regex: , options: } }
đầu vào | Chuỗi mà bạn muốn áp dụng mẫu regex. Có thể là một chuỗi hoặc bất kỳ biểu thức hợp lệ nào được giải quyết thành một chuỗi.regex pattern. Can be a string or any valid expression that resolves to a string. | ||||||||||||
REGEX | Các mẫu regex để áp dụng. Có thể là bất kỳ biểu thức hợp lệ nào được giải quyết thành một chuỗi hoặc mẫu regex 4. Khi sử dụng regex 4, bạn cũng có thể chỉ định các tùy chọn regex 6 và 7 [nhưng không phải là tùy chọn 8 hoặc 9]:
Ngoài ra, bạn cũng có thể chỉ định các tùy chọn Regex với trường Tùy chọn. Để chỉ định các tùy chọn 8 hoặc 9, bạn phải sử dụng trường Tùy chọn.options field. To specify the 8 or 9 options, you must use the options field.Bạn không thể chỉ định các tùy chọn trong cả trường 5 và 6.
| ||||||||||||
tùy chọn | Không bắt buộc. Các 7 sau đây có sẵn để sử dụng với biểu thức thông thường.Ghi chúBạn không thể chỉ định các tùy chọn trong cả trường 5 và 6.
|
db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ] | đầu vào | ||||||
db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ] | Chuỗi mà bạn muốn áp dụng mẫu regex. Có thể là một chuỗi hoặc bất kỳ biểu thức hợp lệ nào được giải quyết thành một chuỗi. REGEX | ||||||
db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ] | Các mẫu regex để áp dụng. Có thể là bất kỳ biểu thức hợp lệ nào được giải quyết thành một chuỗi hoặc mẫu regex 4. Khi sử dụng regex 4, bạn cũng có thể chỉ định các tùy chọn regex 6 và 7 [nhưng không phải là tùy chọn 8 hoặc 9]:Ngoài ra, bạn cũng có thể chỉ định các tùy chọn Regex với trường Tùy chọn. Để chỉ định các tùy chọn 8 hoặc 9, bạn phải sử dụng trường Tùy chọn.Bạn không thể chỉ định các tùy chọn trong cả trường 5 và 6.tùy chọn | ||||||
db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ] | Không bắt buộc. Các 7 sau đây có sẵn để sử dụng với biểu thức thông thường.tùy chọn |
Để cho phép ký tự DOT [nghĩa là
{ "_id" : 1, "category" : "café", "results" : false } { "_id" : 2, "category" : "cafe", "results" : true } { "_id" : 3, "category" : "cafE", "results" : false }
2] trong mẫu để khớp với tất cả các ký tự bao gồm ký tự dòng mới, bao gồm tùy chọn S trong trường Tùy chọn:Ví dụ sau đây bao gồm tùy chọn
8 để cho phép ký tự DOT [tức là.] Để khớp với tất cả các ký tự bao gồm dòng mới cũng như tùy chọndb.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
6 để thực hiện phù hợp không nhạy cảm với trường hợp:db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
Tạo một bộ sưu tập mẫu
02 với các tài liệu sau:db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
Tập hợp sau đây sử dụng db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 để kiểm tra xem trường db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
04 có chứa địa chỉ email với db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
05 không và phân loại phản hồi là db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
06 hoặc db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
07.
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
Ví dụ sau đây bao gồm tùy chọn db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ] db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
8 để cho phép ký tự DOT [tức là.] Để khớp với tất cả các ký tự bao gồm dòng mới cũng như tùy chọn
{ "_id" : 1, "category" : "café", "results" : false } { "_id" : 2, "category" : "cafe", "results" : true } { "_id" : 3, "category" : "cafE", "results" : false }
7, and the index, if used.Tạo một bộ sưu tập mẫu
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
02 với các tài liệu sau:db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
Tập hợp sau đây sử dụng
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 để kiểm tra xem trường db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
04 có chứa địa chỉ email với db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
05 không và phân loại phản hồi là db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
06 hoặc db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
07.db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
Hoạt động trả về các tài liệu sau:
db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 bỏ qua đối chiếu được chỉ định cho bộ sưu tập, { "_id" : 1, "category" : "café", "results" : false } { "_id" : 2, "category" : "cafe", "results" : true } { "_id" : 3, "category" : "cafE", "results" : false }
7 và chỉ số, nếu được sử dụng.{ "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
Ví dụ, việc tạo một bộ sưu tập mẫu với cường độ đối chiếu
{ "_id" : 1, "category" : "café", "results" : false } { "_id" : 2, "category" : "cafe", "results" : true } { "_id" : 3, "category" : "cafE", "results" : false }
8 [nghĩa là chỉ so sánh ký tự cơ sở và bỏ qua các khác biệt khác như trường hợp và dấu hiệu]:db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 ignores collation; that is, the following regular expression pattern matching examples are case-sensitive and diacritic
sensitive:db.myColl.aggregate[ [ { $addFields: { results: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ] ] db.myColl.aggregate[ [ { $addFields: { results: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ], { collation: { locale: "fr", strength: 1 } } // Ignored in the $regexMatch ]
Chèn các tài liệu sau:
{ "_id" : 1, "category" : "café", "results" : false } { "_id" : 2, "category" : "cafe", "results" : true } { "_id" : 3, "category" : "cafE", "results" : false }
Sử dụng đối chiếu của bộ sưu tập, thao tác sau đây thực hiện một trận đấu không nhạy cảm với trường hợp và không nhạy cảm với các trường hợp: db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ] db.myColl.aggregate[ [ { $match: { category: "cafe" } } ] ]
6 Option instead. See
6 Option for an example.
Để minh họa hành vi của toán tử
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 như đã thảo luận trong ví dụ này, hãy tạo một bộ sưu tập mẫu db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
3 với các tài liệu sau:db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 operator as discussed in this example, create a sample collection
db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
3 with the following documents:db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
Theo mặc định,
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 thực hiện một trận đấu nhạy cảm trường hợp. Ví dụ: tập hợp sau đây thực hiện db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 nhạy cảm với trường hợp trên trường db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
6. Mẫu regex db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
7 không chỉ định bất kỳ nhóm nào:db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 performs a case-sensitive match. For example, the following aggregation performs a case-sensitive
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 on the db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
6 field. The regex pattern db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
7 does not specify any grouping:db.products.aggregate[[ { $addFields: { result: { $regexMatch: { input: "$description", regex: /line/ } } } } ]]
Hoạt động trả về như sau:
{ "_id" : 1, "description" : "Single LINE description.", "result" : false } { "_id" : 2, "description" : "First lines\nsecond line", "result" : true } { "_id" : 3, "description" : "Many spaces before line", "result" : true } { "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true } { "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false } { "_id" : 6, "description" : "métier work vocation", "result" : false }
Mẫu Regex sau đây
db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
8 chỉ định nhóm db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
9 trong mẫu:db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
0Hoạt động trả về như sau:
Mẫu Regex sau đây
db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
8 chỉ định nhóm db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
9 trong mẫu: db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
1
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
Ghi chú
Bạn không thể chỉ định các tùy chọn trong cả trường
{ "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
5 và { "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
6.i option as part of the
regex field or in the options field:db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
2Để thực hiện khớp mẫu không nhạy cảm trường hợp, bao gồm tùy chọn I là một phần của trường regex hoặc trong trường Tùy chọn:
7 on the db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
6 field. The regex pattern db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
7 does not specify any grouping:db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
3Hoạt động trả về các tài liệu sau:
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
4 db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
1
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
Ghi chú
Bạn không thể chỉ định các tùy chọn trong cả trường
{ "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
5 và { "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
6.m option as part of the regex field or in the
options field:db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
5Để thực hiện khớp mẫu không nhạy cảm trường hợp, bao gồm tùy chọn I là một phần của trường regex hoặc trong trường Tùy chọn:
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
6Hoạt động trả về như sau:
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
7 db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
1
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
Ghi chú
Bạn không thể chỉ định các tùy chọn trong cả trường
{ "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
5 và { "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
6.s option in the
options field:db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
8Để thực hiện khớp mẫu không nhạy cảm trường hợp, bao gồm tùy chọn I là một phần của trường regex hoặc trong trường Tùy chọn:
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
9Hoạt động trả về như sau:
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
0 db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
1
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
Ghi chú
Bạn không thể chỉ định các tùy chọn trong cả trường
{ "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
5 và { "_id" : 1, "category" : "café" } { "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" }
6.s option in the options
field:db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
1Để thực hiện khớp mẫu không nhạy cảm trường hợp, bao gồm tùy chọn I là một phần của trường regex hoặc trong trường Tùy chọn:
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
2Hoạt động trả về như sau:
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
3Mẫu Regex sau đây
db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
8 chỉ định nhóm db.products.insertMany[[ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" } ]]
9 trong mẫu:db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
4
db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
1db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
7 to check if the db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
04 field contains an email address with db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
05 and categorize the feedback as db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
06 or db.createCollection[ "myColl", { collation: { locale: "fr", strength: 1 } } ]
07.db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
5Hoạt động trả về các tài liệu sau:
db.myColl.insertMany[[ { _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" } ]]
6