feat(mongodb): support createIndex shell command (#2187)
This commit is contained in:
1 parent
4e012a9dd9
commit
b90e4fd64e
17 files changed
+374
-18
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use mongodb::{
|
||||
bson::{doc, oid::ObjectId, Bson, DateTime, Document},
|
||||
options::ClientOptions,
|
||||
options::{ClientOptions, IndexOptions},
|
||||
Client, IndexModel,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -282,6 +282,36 @@ pub async fn aggregate_documents(
|
||||
Ok(MongoDocumentResult { documents, total })
|
||||
}
|
||||
|
||||
pub async fn create_index(
|
||||
client: &Client,
|
||||
database: &str,
|
||||
collection: &str,
|
||||
keys_json: &str,
|
||||
options_json: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
let keys_value: serde_json::Value =
|
||||
serde_json::from_str(keys_json).map_err(|e| format!("Invalid index keys JSON: {e}"))?;
|
||||
let keys = json_object_to_document(&keys_value).map_err(|e| format!("Invalid index keys: {e}"))?;
|
||||
if keys.is_empty() {
|
||||
return Err("Index keys are required".to_string());
|
||||
}
|
||||
|
||||
let options = match options_json.map(str::trim).filter(|json| !json.is_empty()) {
|
||||
Some(json) => {
|
||||
let value: serde_json::Value =
|
||||
serde_json::from_str(json).map_err(|e| format!("Invalid index options JSON: {e}"))?;
|
||||
let doc = json_object_to_document(&value).map_err(|e| format!("Invalid index options: {e}"))?;
|
||||
Some(mongodb::bson::from_document::<IndexOptions>(doc).map_err(|e| format!("Invalid index options: {e}"))?)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
let col = client.database(database).collection::<Document>(collection);
|
||||
let result =
|
||||
col.create_index(IndexModel::builder().keys(keys).options(options).build()).await.map_err(|e| e.to_string())?;
|
||||
Ok(result.index_name)
|
||||
}
|
||||
|
||||
pub async fn insert_document(
|
||||
client: &Client,
|
||||
database: &str,
|
||||
@@ -689,7 +719,6 @@ fn expand_object_id_string_array(items: &[serde_json::Value]) -> Bson {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use mongodb::options::IndexOptions;
|
||||
|
||||
#[test]
|
||||
fn multi_seed_uri_removes_direct_connection_true_before_driver_parse() {
|
||||
|
||||
@@ -223,6 +223,25 @@ pub async fn mongo_aggregate_documents_core(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mongo_create_index_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
collection: &str,
|
||||
keys_json: &str,
|
||||
options_json: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => {
|
||||
mongo_driver::create_index(client, database, collection, keys_json, options_json).await
|
||||
}
|
||||
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support createIndex".to_string()),
|
||||
_ => Err("Not a MongoDB connection".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mongo_insert_document_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
|
||||
Reference in new issue
Block a user