mongodb 0.1.4 bindings for Rust提供GridFS实现。
从代码和示例中可以看到put
,但它不会返回对象ID。
我的解决方法是将文件放入GridFS,然后再次打开以检索ID:
fn file_to_mongo(gridfs: &Store, fpath: &PathBuf) -> bson::oid::ObjectId {
gridfs.put(fpath.to_str().unwrap().to_owned());
let mut file = gridfs.open(fpath.to_str().unwrap().to_owned()).unwrap();
let id = file.doc.id.clone();
file.close().unwrap();
id
}
有更好的方法吗?
答案 0 :(得分:1)
我没有运行MongoDB而且我对此一无所知,但这至少有正确的签名和编译。
extern crate bson;
extern crate mongodb;
use mongodb::gridfs::{Store,ThreadedStore};
use mongodb::error::Result as MongoResult;
use std::{fs, io};
fn my_put(store: &Store, name: String) -> MongoResult<bson::oid::ObjectId> {
let mut f = try!(fs::File::open(&name));
let mut file = try!(store.create(name));
try!(io::copy(&mut f, &mut file));
try!(file.close());
Ok(file.doc.id.clone())
}
回想一下,大多数Rust库都是开源的,您甚至可以直接从文档中浏览源代码。此功能基本上只是现有put
的黑客版本。