diff --git a/src/collection.rs b/src/collection.rs index 1164eb2..ac6d4a0 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -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 { + 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( &self, query: &Document, diff --git a/src/database.rs b/src/database.rs index 1a604a4..614e593 100644 --- a/src/database.rs +++ b/src/database.rs @@ -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 { + 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>>( &self, name: S, diff --git a/tests/collection.rs b/tests/collection.rs index 51ca2f0..31cb2f9 100644 --- a/tests/collection.rs +++ b/tests/collection.rs @@ -18,6 +18,19 @@ fn test_command() { 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] fn test_mutation_and_finding() { let uri = Uri::new("mongodb://localhost:27017/").unwrap(); diff --git a/tests/database.rs b/tests/database.rs index 6e744dd..b255bfa 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -14,6 +14,19 @@ fn test_command() { 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] fn test_get_collection_and_name() { let uri = Uri::new("mongodb://localhost:27017/").unwrap();