Skip to content

Documentation: Updating example #542

@GreeFine

Description

@GreeFine

I just had a lot of trouble finding how to update a document in my collection, which has a value that isn't primitive (a structure here in my example).

I was trying to set the "oders" fields.
And I had to look for some time to find that you can use bson::to_bson to Sereliaze the structure.

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Turtle {
    name: String,
    pub orders: Vec<Command>,
    pub infos: HashMap<String, String>,
    pos: Position,
    facing: Facing,
}


let result = turtles
        .update_one(
            doc! { "name": &name },
            doc! { "$set": { "orders": bson::to_bson(&orders).expect("Unable to convert orders to bson") } },
            None,
        )
        .await
        .expect("Unable to update orders of the turtle");

Could we add an example about bson::to_bson and perhaps also about updating in the README.md usage section ?
I can do it and open a Pull requests if it helps 👍

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions