Fix passing of update in find_and_modify

pull/4/head
Thijs Cadier 9 years ago
parent b536f1c048
commit c2338a389f

@ -369,25 +369,31 @@ impl<'a> Collection<'a> {
// Do these before the mongoc call to make sure we keep // Do these before the mongoc call to make sure we keep
// them around long enough. // them around long enough.
let sort = match options.sort { let sort_bsonc = match options.sort {
Some(ref doc) => { Some(ref doc) => {
try!(Bsonc::from_document(doc)).inner() Some(try!(Bsonc::from_document(doc)))
}, },
None => ptr::null() None => None
}; };
let update = match operation { let update_bsonc = match operation {
FindAndModifyOperation::Update(ref doc) | FindAndModifyOperation::Upsert(ref doc) => { FindAndModifyOperation::Update(ref doc) | FindAndModifyOperation::Upsert(ref doc) => {
try!(Bsonc::from_document(doc)).inner() Some(try!(Bsonc::from_document(doc)))
}, },
FindAndModifyOperation::Remove => ptr::null() FindAndModifyOperation::Remove => None
}; };
let success = unsafe { let success = unsafe {
bindings::mongoc_collection_find_and_modify( bindings::mongoc_collection_find_and_modify(
self.inner, self.inner,
try!(Bsonc::from_document(&query)).inner(), try!(Bsonc::from_document(&query)).inner(),
sort, match sort_bsonc {
update, Some(ref s) => s.inner(),
None => ptr::null()
},
match update_bsonc {
Some(ref u) => u.inner(),
None => ptr::null()
},
match fields_bsonc { match fields_bsonc {
Some(ref f) => f.inner(), Some(ref f) => f.inner(),
None => ptr::null() None => ptr::null()

@ -140,7 +140,7 @@ fn test_find_and_modify() {
"key_1" => "Value 1" "key_1" => "Value 1"
}; };
let update = doc! { let update = doc! {
"$set" => {"content" => 1} "$set" => {"content" => 1i32}
}; };
let result = collection.find_and_modify( let result = collection.find_and_modify(
&query, &query,
@ -148,12 +148,13 @@ fn test_find_and_modify() {
None None
); );
assert!(result.is_ok()); assert!(result.is_ok());
assert_eq!(update.get("content"), result.unwrap().get("content"));
assert_eq!(1, collection.count(&query, None).unwrap()); assert_eq!(1, collection.count(&query, None).unwrap());
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
assert_eq!(found_document.get_i32("content"), Ok(1));
// Update this record // Update this record
let update2 = doc! { let update2 = doc! {
"$set" => {"content" => 2} "$set" => {"content" => 2i32}
}; };
let result = collection.find_and_modify( let result = collection.find_and_modify(
&query, &query,
@ -163,7 +164,7 @@ fn test_find_and_modify() {
assert!(result.is_ok()); assert!(result.is_ok());
assert_eq!(1, collection.count(&query, None).unwrap()); assert_eq!(1, collection.count(&query, None).unwrap());
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap(); let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
assert_eq!(update2.get("content"), found_document.get("content")); assert_eq!(found_document.get_i32("content"), Ok(2));
// Remove it // Remove it
let result = collection.find_and_modify( let result = collection.find_and_modify(

Loading…
Cancel
Save