fix(mongodb): support collation for find queries
This commit is contained in:
21 files changed
+373
-30
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use mongodb::{
|
||||
bson::{doc, oid::ObjectId, Bson, DateTime, Document},
|
||||
options::{ClientOptions, GridFsBucketOptions, IndexOptions, UpdateModifications},
|
||||
options::{ClientOptions, Collation, GridFsBucketOptions, IndexOptions, UpdateModifications},
|
||||
Client, Cursor, Database, IndexModel,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -16,6 +16,18 @@ pub use super::document_result::DocumentQueryResult;
|
||||
/// Backward-compatible name for callers of Mongo-specific APIs.
|
||||
pub type MongoDocumentResult = DocumentQueryResult;
|
||||
|
||||
const MONGO_COLLATION_FIELDS: &[&str] = &[
|
||||
"locale",
|
||||
"strength",
|
||||
"caseLevel",
|
||||
"caseFirst",
|
||||
"numericOrdering",
|
||||
"alternate",
|
||||
"maxVariable",
|
||||
"normalization",
|
||||
"backwards",
|
||||
];
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MongoDropIndexesResult {
|
||||
pub dropped_names: Vec<String>,
|
||||
@@ -636,6 +648,23 @@ fn index_info_from_model(model: IndexModel) -> IndexInfo {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_find_collation(value: Option<&str>) -> Result<Option<Collation>, String> {
|
||||
let Some(raw) = value.map(str::trim).filter(|value| !value.is_empty()) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let json: serde_json::Value =
|
||||
serde_json::from_str(raw).map_err(|error| format!("Invalid collation JSON: {error}"))?;
|
||||
let object = json.as_object().ok_or_else(|| "Invalid collation: expected an options object".to_string())?;
|
||||
if let Some(field) = object.keys().find(|field| !MONGO_COLLATION_FIELDS.contains(&field.as_str())) {
|
||||
return Err(format!("Unsupported collation option: {field}"));
|
||||
}
|
||||
let collation: Collation = serde_json::from_value(json).map_err(|error| format!("Invalid collation: {error}"))?;
|
||||
if collation.locale.trim().is_empty() {
|
||||
return Err("Invalid collation: locale must not be empty".to_string());
|
||||
}
|
||||
Ok(Some(collation))
|
||||
}
|
||||
|
||||
pub async fn find_documents(
|
||||
client: &Client,
|
||||
database: &str,
|
||||
@@ -645,6 +674,7 @@ pub async fn find_documents(
|
||||
filter: Option<&str>,
|
||||
projection: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
collation: Option<&str>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
let col = client.database(database).collection::<Document>(collection);
|
||||
|
||||
@@ -656,9 +686,14 @@ pub async fn find_documents(
|
||||
_ => doc! {},
|
||||
};
|
||||
|
||||
let collation = parse_find_collation(collation)?;
|
||||
let count_is_exact = !filter_doc.is_empty();
|
||||
let total_result = if count_is_exact {
|
||||
col.count_documents(filter_doc.clone()).await.map_err(|e| e.to_string())
|
||||
let mut count = col.count_documents(filter_doc.clone());
|
||||
if let Some(collation) = collation.clone() {
|
||||
count = count.collation(collation);
|
||||
}
|
||||
count.await.map_err(|e| e.to_string())
|
||||
} else {
|
||||
col.estimated_document_count().await.map_err(|e| e.to_string())
|
||||
};
|
||||
@@ -679,6 +714,9 @@ pub async fn find_documents(
|
||||
find = find.sort(sort_doc);
|
||||
}
|
||||
}
|
||||
if let Some(collation) = collation {
|
||||
find = find.collation(collation);
|
||||
}
|
||||
|
||||
let mut cursor = find.await.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -791,6 +829,7 @@ pub async fn find_documents_extended_json(
|
||||
filter: Option<&str>,
|
||||
projection: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
collation: Option<&str>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
let col = client.database(database).collection::<Document>(collection);
|
||||
|
||||
@@ -802,9 +841,14 @@ pub async fn find_documents_extended_json(
|
||||
_ => doc! {},
|
||||
};
|
||||
|
||||
let collation = parse_find_collation(collation)?;
|
||||
let count_is_exact = !filter_doc.is_empty();
|
||||
let total_result = if count_is_exact {
|
||||
col.count_documents(filter_doc.clone()).await.map_err(|e| e.to_string())
|
||||
let mut count = col.count_documents(filter_doc.clone());
|
||||
if let Some(collation) = collation.clone() {
|
||||
count = count.collation(collation);
|
||||
}
|
||||
count.await.map_err(|e| e.to_string())
|
||||
} else {
|
||||
col.estimated_document_count().await.map_err(|e| e.to_string())
|
||||
};
|
||||
@@ -825,6 +869,9 @@ pub async fn find_documents_extended_json(
|
||||
find = find.sort(sort_doc);
|
||||
}
|
||||
}
|
||||
if let Some(collation) = collation {
|
||||
find = find.collation(collation);
|
||||
}
|
||||
|
||||
let mut cursor = find.await.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -2088,6 +2135,37 @@ fn expand_object_id_string_array(items: &[serde_json::Value]) -> Bson {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_find_collation_options() {
|
||||
let collation = parse_find_collation(Some(
|
||||
r#"{"locale":"en","strength":1,"caseLevel":true,"caseFirst":"upper","numericOrdering":true,"alternate":"shifted","maxVariable":"space","normalization":true,"backwards":false}"#,
|
||||
))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(collation.locale, "en");
|
||||
assert!(matches!(collation.strength, Some(mongodb::options::CollationStrength::Primary)));
|
||||
assert_eq!(collation.case_level, Some(true));
|
||||
assert!(matches!(collation.case_first, Some(mongodb::options::CollationCaseFirst::Upper)));
|
||||
assert_eq!(collation.numeric_ordering, Some(true));
|
||||
assert!(matches!(collation.alternate, Some(mongodb::options::CollationAlternate::Shifted)));
|
||||
assert!(matches!(collation.max_variable, Some(mongodb::options::CollationMaxVariable::Space)));
|
||||
assert_eq!(collation.normalization, Some(true));
|
||||
assert_eq!(collation.backwards, Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_find_collation_options() {
|
||||
assert!(parse_find_collation(Some(r#"{"strength":1}"#)).unwrap_err().contains("locale"));
|
||||
assert!(parse_find_collation(Some(r#"{"locale":""}"#)).unwrap_err().contains("locale"));
|
||||
assert!(parse_find_collation(Some(r#"{"locale":"en","unknown":true}"#))
|
||||
.unwrap_err()
|
||||
.contains("Unsupported collation option"));
|
||||
assert!(parse_find_collation(Some(r#"{"locale":"en","strength":"primary"}"#))
|
||||
.unwrap_err()
|
||||
.contains("Invalid collation"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mongo_find_count_failure_returns_loaded_lower_bound() {
|
||||
let error = "invalid type: floating point `2053278871.0`, expected u64".to_string();
|
||||
|
||||
@@ -402,6 +402,7 @@ pub async fn find_documents_core(
|
||||
filter: Option<&str>,
|
||||
projection: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
collation: Option<&str>,
|
||||
) -> Result<DocumentQueryResult, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
@@ -410,7 +411,7 @@ pub async fn find_documents_core(
|
||||
// Document browser responses must retain BSON type metadata so nested filters
|
||||
// can round-trip ObjectId, Date, and int64 values through Extended JSON.
|
||||
mongo_driver::find_documents_extended_json(
|
||||
client, database, collection, skip, limit, filter, projection, sort,
|
||||
client, database, collection, skip, limit, filter, projection, sort, collation,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -443,6 +444,9 @@ pub async fn find_documents_core(
|
||||
if let Some(projection) = projection {
|
||||
params["projection"] = serde_json::json!(projection);
|
||||
}
|
||||
if let Some(collation) = collation {
|
||||
params["collation"] = serde_json::json!(collation);
|
||||
}
|
||||
match client.mongo_find_documents_extended_json(params.clone()).await {
|
||||
Ok(result) => Ok(result),
|
||||
Err(error) if is_unknown_agent_method_error(&error, "find_documents_extended_json") => {
|
||||
|
||||
@@ -138,6 +138,7 @@ pub async fn mongo_find_documents_core(
|
||||
filter: Option<&str>,
|
||||
projection: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
collation: Option<&str>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
crate::document_ops::find_documents_core(
|
||||
state,
|
||||
@@ -149,6 +150,7 @@ pub async fn mongo_find_documents_core(
|
||||
filter,
|
||||
projection,
|
||||
sort,
|
||||
collation,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -236,7 +238,7 @@ pub async fn mongo_find_documents_extended_json_core(
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => {
|
||||
mongo_driver::find_documents_extended_json(
|
||||
client, database, collection, skip, limit, filter, projection, sort,
|
||||
client, database, collection, skip, limit, filter, projection, sort, None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -9,7 +9,15 @@ pub enum MongoCommand {
|
||||
#[serde(rename = "use")]
|
||||
Use { database: String },
|
||||
#[serde(rename = "find")]
|
||||
Find { collection: String, filter: String, projection: Option<String>, sort: Option<String>, skip: u64, limit: i64 },
|
||||
Find {
|
||||
collection: String,
|
||||
filter: String,
|
||||
projection: Option<String>,
|
||||
sort: Option<String>,
|
||||
collation: Option<String>,
|
||||
skip: u64,
|
||||
limit: i64,
|
||||
},
|
||||
#[serde(rename = "findOne")]
|
||||
FindOne { collection: String, filter: String, projection: Option<String>, options: Option<String> },
|
||||
#[serde(rename = "countDocuments")]
|
||||
@@ -339,11 +347,18 @@ pub fn parse(input: &str) -> Result<MongoCommand, String> {
|
||||
return Err("MongoDB find() accepts at most filter and projection arguments.".to_string());
|
||||
}
|
||||
let mut sort = None;
|
||||
let mut collation = None;
|
||||
let mut skip = 0;
|
||||
let mut limit = 100;
|
||||
for (name, call_args) in chained_calls(&tail)? {
|
||||
match name.as_str() {
|
||||
"sort" => sort = Some(normalized_json(call_args.first().map(String::as_str).unwrap_or("{}"))?),
|
||||
"collation" => {
|
||||
if call_args.len() != 1 {
|
||||
return Err("MongoDB collation() requires one options object.".to_string());
|
||||
}
|
||||
collation = Some(normalized_json(&call_args[0])?);
|
||||
}
|
||||
"skip" => skip = parse_integer(&call_args, "skip")? as u64,
|
||||
"limit" => limit = parse_integer(&call_args, "limit")?,
|
||||
"count" if call_args.is_empty() => {
|
||||
@@ -352,7 +367,7 @@ pub fn parse(input: &str) -> Result<MongoCommand, String> {
|
||||
_ => return Err(format!("Unsupported MongoDB find() chain: {name}()")),
|
||||
}
|
||||
}
|
||||
return Ok(MongoCommand::Find { collection, filter, projection, sort, skip, limit });
|
||||
return Ok(MongoCommand::Find { collection, filter, projection, sort, collation, skip, limit });
|
||||
}
|
||||
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "findOne") {
|
||||
@@ -822,12 +837,29 @@ mod tests {
|
||||
filter: r#"{"_id":{"$oid":"507f1f77bcf86cd799439011"}}"#.to_string(),
|
||||
projection: Some(r#"{"title":1,"_id":0}"#.to_string()),
|
||||
sort: Some(r#"{"title":1}"#.to_string()),
|
||||
collation: None,
|
||||
skip: 0,
|
||||
limit: 1,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_find_with_collation_chain() {
|
||||
assert_eq!(
|
||||
parse(r#"db.t_user.find({name: 'xxx'}).collation({ locale: "en", strength: 1 }).limit(20)"#).unwrap(),
|
||||
MongoCommand::Find {
|
||||
collection: "t_user".to_string(),
|
||||
filter: r#"{"name":"xxx"}"#.to_string(),
|
||||
projection: None,
|
||||
sort: None,
|
||||
collation: Some(r#"{"locale":"en","strength":1}"#.to_string()),
|
||||
skip: 0,
|
||||
limit: 20,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_get_collection_and_count() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -3716,6 +3716,7 @@ async fn find_mongo_documents_for_rows(
|
||||
None,
|
||||
None,
|
||||
Some(r#"{"_id":1}"#),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user