MongoDB
MongoDB services support.
Capabilities
This service can be used to:
- stat
- read
- write
- create_dir
- delete
- copy
- rename
-
list -
presign - blocking
Configuration
root
: Set the working directory ofOpenDAL
connection_string
: Set the connection string of mongodb serverdatabase
: Set the database of mongodbcollection
: Set the collection of mongodbkey_field
: Set the key field of mongodbvalue_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
- Rust
- Node.js
- Python
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(())
}
import { Operator } from "opendal";
async function main() {
const op = new Operator("mongodb", {
root: "/",
connection_string: "mongodb://myUser:myPassword@localhost:27017/myAuthDB",
database: "your_database",
collection: "your_collection",
key_field: "key",
value_field: "value"
});
}
import opendal
op = opendal.Operator("mongodb",
root="/",
connection_string="mongodb://myUser:myPassword@localhost:27017/myAuthDB",
database="your_database",
collection="your_collection",
key_field="key",
value_field="value"
)