使用rust操作mongodb
准备工作 使用rust进行mongodb连接需要添加依赖,在Cargo.toml中添加下面的依赖 [dependencies] mongodb="2" serde = "1" 添加serde的原因是我们建模的时候需要用。 预备知识 连接数据库 下面是我们的本地数据库连接 const CONNECT_STR:&str = "mongodb://czyt:[email protected]:27017"; 然后使用连接字符串进行数据库连接 let mut client_options = ClientOptions::parse_async(CONNECT_STR).await?; // Set the server_api field of the client_options object to Stable API version 1 let server_api = ServerApi::builder() .version(ServerApiVersion::V1) .build(); client_options.server_api = Some(server_api); // Create a new client and connect to the server let client = Client::with_options(client_options)?; // Send a ping to confirm a successful connection client.database("admin").run_command(doc! { "ping": 1 }, None)....