fix(mongodb): support shell regex literals

This commit is contained in:
Guoyu Su authored and GitHub committed 2026-09-02 20:36:59 +08:00
1 parent d6c4457756
commit d92e119464
4 files changed
+638 -8

No files matched your search

+24
View File
@@ -2655,6 +2655,11 @@ fn json_filter_value_to_bson(value: &serde_json::Value, field_name: Option<&str>
}
serde_json::Value::Object(obj) => {
if obj.len() == 1 {
if obj.contains_key("$regularExpression") {
if let Ok(Some(value)) = parse_extended_json_value(obj) {
return value;
}
}
if let Some(serde_json::Value::String(hex)) = obj.get("$oid") {
if let Ok(oid) = ObjectId::parse_str(hex) {
return Bson::ObjectId(oid);
@@ -3968,6 +3973,25 @@ mod tests {
assert!(matches!(doc.get("count"), Some(Bson::Int64(42))));
}
#[test]
fn json_filter_to_document_parses_extended_json_regex() {
let value = serde_json::json!({
"packagingRatio": {
"$regularExpression": {
"pattern": "^[^::]*盒",
"options": "im",
}
}
});
let doc = json_filter_to_document(&value).unwrap();
assert!(matches!(
doc.get("packagingRatio"),
Some(Bson::RegularExpression(regex))
if regex.pattern == "^[^::]*盒" && regex.options == "im"
));
}
#[test]
fn json_object_to_document_extended_json_keeps_shell_date_strings_literal() {
let value = serde_json::json!({