Add command_simple

pull/4/head
Thijs Cadier 9 years ago
parent dcf7db62a3
commit 72cb5be78f

@ -161,6 +161,47 @@ impl<'a> Collection<'a> {
)) ))
} }
/// Simplified version of command that returns the first document
///
/// See: http://api.mongodb.org/c/current/mongoc_database_command_simple.html
pub fn command_simple(
&'a self,
command: Document,
options: Option<&CommandAndFindOptions>
) -> Result<Document> {
assert!(!self.inner.is_null());
let default_options = CommandAndFindOptions::default();
let options = options.unwrap_or(&default_options);
// Bsonc to store the reply
let mut reply = Bsonc::new();
// Empty error that might be filled
let mut error = BsoncError::empty();
let success = unsafe {
bindings::mongoc_collection_command_simple(
self.inner,
try!(Bsonc::from_document(&command)).inner(),
match options.read_prefs {
Some(ref prefs) => prefs.inner(),
None => ptr::null()
},
reply.mut_inner(),
error.mut_inner()
)
};
if success == 1 {
match reply.as_document() {
Ok(document) => return Ok(document),
Err(error) => return Err(error.into())
}
} else {
Err(error.into())
}
}
pub fn count( pub fn count(
&self, &self,
query: &Document, query: &Document,

@ -77,6 +77,47 @@ impl<'a> Database<'a> {
)) ))
} }
/// Simplified version of command that returns the first document
///
/// See: http://api.mongodb.org/c/current/mongoc_database_command_simple.html
pub fn command_simple(
&'a self,
command: Document,
options: Option<&CommandAndFindOptions>
) -> Result<Document> {
assert!(!self.inner.is_null());
let default_options = CommandAndFindOptions::default();
let options = options.unwrap_or(&default_options);
// Bsonc to store the reply
let mut reply = Bsonc::new();
// Empty error that might be filled
let mut error = BsoncError::empty();
let success = unsafe {
bindings::mongoc_database_command_simple(
self.inner,
try!(Bsonc::from_document(&command)).inner(),
match options.read_prefs {
Some(ref prefs) => prefs.inner(),
None => ptr::null()
},
reply.mut_inner(),
error.mut_inner()
)
};
if success == 1 {
match reply.as_document() {
Ok(document) => return Ok(document),
Err(error) => return Err(error.into())
}
} else {
Err(error.into())
}
}
pub fn create_collection<S: Into<Vec<u8>>>( pub fn create_collection<S: Into<Vec<u8>>>(
&self, &self,
name: S, name: S,

@ -18,6 +18,19 @@ fn test_command() {
assert!(result.contains_key("ok")); assert!(result.contains_key("ok"));
} }
#[test]
fn test_command_simple() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None);
let client = pool.pop();
let collection = client.get_collection("rust_driver_test", "items");
let command = doc! { "ping" => 1 };
let result = collection.command_simple(command, None).unwrap();
assert!(result.contains_key("ok"));
}
#[test] #[test]
fn test_mutation_and_finding() { fn test_mutation_and_finding() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new("mongodb://localhost:27017/").unwrap();

@ -14,6 +14,19 @@ fn test_command() {
assert!(result.contains_key("ok")); assert!(result.contains_key("ok"));
} }
#[test]
fn test_command_simple() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None);
let client = pool.pop();
let database = client.get_database("rust_test");
let command = doc! { "ping" => 1 };
let result = database.command_simple(command, None).unwrap();
assert!(result.contains_key("ok"));
}
#[test] #[test]
fn test_get_collection_and_name() { fn test_get_collection_and_name() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new("mongodb://localhost:27017/").unwrap();

Loading…
Cancel
Save