Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSON likes Strings and hates u32/usize #70

Closed
TrionProg opened this issue May 17, 2017 · 1 comment
Closed

BSON likes Strings and hates u32/usize #70

TrionProg opened this issue May 17, 2017 · 1 comment
Assignees

Comments

@TrionProg
Copy link

TrionProg commented May 17, 2017

Hello. I have struct

pub struct Award{
    pub name:String,
    pub icon:Uuid,
    pub description:String,
    pub id:String,
}

And I write it to mongodb

                let award=Award{
                //id:id as u32,
                name:String::from(award_name),
                icon:icon,
                description:description,
                id:id .to_string(),
            };

            let serialized_award = bson::to_bson(&award)?;

            let find_filter=doc! {
                "_id" => user_id
            };

            let update_doc=doc! {//All right?
                "$push" => { "awards" => serialized_award }
            };

            mongo_users.find_one_and_update( find_filter ,update_doc, None );//TODO:None

And later I read:

let doc=mongo_users.find_one(Some(find_filter),Some(find_option))?;

        println!("{:?}",doc);

        match doc {
            Some( doc ) => {
               match doc.get("awards") {
                Some(&Bson::Array(ref mongo_awards)) => {

                let mut awards=Vec::with_capacity(mongo_awards.len());
                for mongo_award in mongo_awards.iter() {
                    match *mongo_award {
                        Bson::Document( ref doc ) => {
                            let award:Award = bson::from_bson( bson::Bson::Document(doc.clone()) ).unwrap();//NOTE : problem, and I DO NOT WANT CLONE document!

Part of mongo document

"awards": Array([Document({ name: "abc", icon: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", description: "def", id: 1 })])

If id of struct Award is String, all works, but if id is u32 or usize I get InvalidType("u32")
Note that Display trait for this error writes only usize,u32 without any description like InvalidType:usize

UPD. Struct initialization in exemple gets &'static str instead String. This code do not works!

#[derive(Serialize, Deserialize, Debug)]
        pub struct Person {
            #[serde(rename = "_id")]  // Use MongoDB's special primary key field name when serializing
            pub id: String,
            pub name: String,
            pub age: u32
        }

        let person = Person {
            id: "12345".to_string(),
            name: "Emma".to_string(),
            age: 3
        };

        let serialized_person = bson::to_bson(&person)?;  // Serialize

        let person2:Person = bson::from_bson(serialized_person).unwrap();
}

Cargo.toml

serde="1.0.2"
serde_derive="*"
bincode="*"
redis = "*"
cdrs = "1.0.0-rc.1"
bson = ">=0.7.0"
mongodb = ">=0.2.7"

i32,i64 works. I guess, bson hates unsigned types.

@zonyitoo zonyitoo added bug and removed bug labels May 18, 2017
@zonyitoo
Copy link
Contributor

zonyitoo commented May 18, 2017

I can reproduce your error in my environment.

the serialized_person in your example is actually something like this:

Document(OrderedDocument({"_id": String("12345"), "name": String("Emma"), "age": FloatingPoint(3)}))

As you can see, age is a FloatingPoint. So why it is serialized to a FloatingPoint instead of a I32? ... Hmm...

The problem is that BSON doesn't support unsigned type.

@zonyitoo zonyitoo self-assigned this May 21, 2017
zonyitoo added a commit that referenced this issue May 30, 2017
* [#70] Disallow serde serialize/deserialize unsigned integer types

* add compat mod for backward compatibility

* Added breaking changes in README
@zonyitoo zonyitoo closed this as completed Jul 3, 2018
lrlna pushed a commit to lrlna/bson-rs that referenced this issue Feb 27, 2019
* [mongodb#70] Disallow serde serialize/deserialize unsigned integer types

* add compat mod for backward compatibility

* Added breaking changes in README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants