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
// them around long enough.
let sort = match options.sort {
let sort_bsonc = match options.sort {
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) => {
try!(Bsonc::from_document(doc)).inner()
Some(try!(Bsonc::from_document(doc)))
},
FindAndModifyOperation::Remove => ptr::null()
FindAndModifyOperation::Remove => None
};
let success = unsafe {
bindings::mongoc_collection_find_and_modify(
self.inner,
try!(Bsonc::from_document(&query)).inner(),
sort,
update,
match sort_bsonc {
Some(ref s) => s.inner(),
None => ptr::null()
},
match update_bsonc {
Some(ref u) => u.inner(),
None => ptr::null()
},
match fields_bsonc {
Some(ref f) => f.inner(),
None => ptr::null()

@ -140,7 +140,7 @@ fn test_find_and_modify() {
"key_1" => "Value 1"
};
let update = doc! {
"$set" => {"content" => 1}
"$set" => {"content" => 1i32}
};
let result = collection.find_and_modify(
&query,
@ -148,12 +148,13 @@ fn test_find_and_modify() {
None
);
assert!(result.is_ok());
assert_eq!(update.get("content"), result.unwrap().get("content"));
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
let update2 = doc! {
"$set" => {"content" => 2}
"$set" => {"content" => 2i32}
};
let result = collection.find_and_modify(
&query,
@ -163,7 +164,7 @@ fn test_find_and_modify() {
assert!(result.is_ok());
assert_eq!(1, collection.count(&query, None).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
let result = collection.find_and_modify(

Loading…
Cancel
Save