feat(mongodb): support copying MongoDB documents

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-24 15:50:49 +08:00
1 parent b8c488b398
commit 0992c52448
19 files changed
+580 -43

No files matched your search

+46 -5
View File
@@ -667,7 +667,7 @@ pub async fn find_documents(
while cursor.advance().await.map_err(|e| e.to_string())? {
let doc = cursor.deserialize_current().map_err(|e| e.to_string())?;
documents.push(bson_to_json(&Bson::Document(doc.clone())));
extended_documents.push(Bson::Document(doc).into_relaxed_extjson());
extended_documents.push(Bson::Document(doc).into_canonical_extjson());
}
Ok(MongoDocumentResult {
@@ -807,13 +807,16 @@ pub async fn find_documents_extended_json(
let mut cursor = find.await.map_err(|e| e.to_string())?;
let mut documents = Vec::new();
let mut extended_documents = Vec::new();
while cursor.advance().await.map_err(|e| e.to_string())? {
let doc = cursor.deserialize_current().map_err(|e| e.to_string())?;
documents.push(bson_to_browser_json(&Bson::Document(doc)));
let (document, extended_document) = document_json_views(doc);
documents.push(document);
extended_documents.push(extended_document);
}
Ok(MongoDocumentResult {
extended_documents: Some(documents.clone()),
extended_documents: Some(extended_documents),
documents,
raw_documents: None,
total,
@@ -1346,7 +1349,7 @@ fn single_document_result(document: Option<Document>) -> MongoDocumentResult {
Some(document) => MongoDocumentResult {
documents: vec![bson_to_json(&Bson::Document(document.clone()))],
raw_documents: None,
extended_documents: Some(vec![Bson::Document(document).into_relaxed_extjson()]),
extended_documents: Some(vec![Bson::Document(document).into_canonical_extjson()]),
total: 1,
total_is_exact: true,
},
@@ -1584,6 +1587,14 @@ fn bson_to_browser_json(bson: &Bson) -> serde_json::Value {
}
}
fn document_json_views(document: Document) -> (serde_json::Value, serde_json::Value) {
let bson = Bson::Document(document);
let browser = bson_to_browser_json(&bson);
// Derive copy JSON from the original BSON so every BSON type keeps its canonical wrapper.
let extended = bson.into_canonical_extjson();
(browser, extended)
}
/// Convert a `serde_json::Value` (JSON object) to a BSON `Document`,
/// handling MongoDB extended JSON conventions such as `{"$oid":"..."}`.
pub fn json_object_to_document(value: &serde_json::Value) -> Result<Document, String> {
@@ -2271,10 +2282,40 @@ mod tests {
assert_eq!(result.documents[0]["lastUpdatedDate"], serde_json::json!("ISODate(\"2025-05-06T08:35:32Z\")"));
let extended = result.extended_documents.expect("extended documents");
assert_eq!(extended[0]["lastUpdatedDate"], serde_json::json!({ "$date": "2025-05-06T08:35:32Z" }));
assert_eq!(extended[0]["lastUpdatedDate"], serde_json::json!({ "$date": { "$numberLong": "1746520532000" } }));
assert_eq!(extended[0]["dateText"], serde_json::json!("ISODate(\"2025-05-06T08:35:32Z\")"));
}
#[test]
fn document_json_views_keep_browser_display_and_canonical_bson_types() {
let date = DateTime::parse_rfc3339_str("2026-06-10T13:59:31.287Z").unwrap();
let (browser, extended) = document_json_views(doc! {
"date": date,
"int32": Bson::Int32(42),
"int64": Bson::Int64(42),
"unsafeInt64": Bson::Int64(2_326_645_729_978_441_729),
});
assert_eq!(
browser,
serde_json::json!({
"date": "ISODate(\"2026-06-10T13:59:31.287Z\")",
"int32": 42,
"int64": 42,
"unsafeInt64": { "$numberLong": "2326645729978441729" },
})
);
assert_eq!(
extended,
serde_json::json!({
"date": { "$date": { "$numberLong": "1781099971287" } },
"int32": { "$numberInt": "42" },
"int64": { "$numberLong": "42" },
"unsafeInt64": { "$numberLong": "2326645729978441729" },
})
);
}
#[test]
fn bson_to_json_preserves_unsafe_int64_for_js() {
let value = bson_to_json(&Bson::Int64(2_326_645_729_978_441_729));