Skip to main content

MongoDB

MongoDB services support.

Capabilities

This service can be used to:

  • stat
  • read
  • write
  • create_dir
  • delete
  • copy
  • rename
  • list
  • scan
  • presign
  • blocking

Configuration

  • root: Set the working directory of OpenDAL
  • connection_string: Set the connection string of mongodb server
  • database: Set the database of mongodb
  • collection: Set the collection of mongodb
  • key_field: Set the key field of mongodb
  • value_field: Set the value field of mongodb

Example

Via Builder

use anyhow::Result;
use opendal::services::Mongodb;
use opendal::Operator;

#[tokio::main]
async fn main() -> Result<()> {
let mut builder = Mongodb::default();
builder.root("/");
builder.connection_string("mongodb://myUser:myPassword@localhost:27017/myAuthDB");
builder.database("your_database");
builder.collection("your_collection");
// key field type in the table should be compatible with Rust's &str like text
builder.key_field("key");
// value field type in the table should be compatible with Rust's Vec<u8> like bytea
builder.value_field("value");

let op = Operator::new(builder)?.finish();
Ok(())
}

Via Config

use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<()> {
let mut map = HashMap::new();
map.insert("root".to_string(), "/".to_string());
map.insert("connection_string".to_string(), "mongodb://myUser:myPassword@localhost:27017/myAuthDB".to_string());
map.insert("database".to_string(), "your_database".to_string());
map.insert("collection".to_string(), "your_collection".to_string());
map.insert("key_field".to_string(), "key".to_string());
map.insert("value_field".to_string(), "value".to_string());

let op: Operator = Operator::via_map(Scheme::Mongodb, map)?;
Ok(())
}