Giter VIP home page Giter VIP logo

Comments (2)

abr-egn avatar abr-egn commented on July 29, 2024

Hi!

I can't reproduce the issue you're seeing. This test program

use bson::doc;
use mongodb::options::FindOptions;
use serde::{Serialize, Deserialize};
use futures::stream::TryStreamExt;

#[derive(Debug, Serialize, Deserialize)]
struct Book {
    title: String,
    author: String,
    problem: u16,
}

#[tokio::main]
async fn main() {
    let book = Book {
        title: "1984".to_string(),
        author: "George Orwell".to_string(),
        problem: 21010,
    };
    println!("original book:    {:?}", book);
    let bson_roundtrip: Book = bson::from_bson(bson::to_bson(&book).unwrap()).unwrap();
    println!("bson roundtrip:   {:?}", bson_roundtrip);
    let binary_roundtrip: Book = bson::from_reader(bson::to_vec(&book).unwrap().as_slice()).unwrap();
    println!("binary roundtrip: {:?}", binary_roundtrip);

    let client = mongodb::Client::with_uri_str("mongodb://localhost:27017").await.unwrap();
    let typed_collection = client.database("test_u16").collection::<Book>("test");
    typed_collection.drop(None).await.unwrap();
    typed_collection.insert_one(&book, None).await.unwrap();

    // Query the books in the collection with a filter and an option.
    let filter = doc! { "author": "George Orwell" };
    let find_options = FindOptions::builder().sort(doc! { "title": 1 }).build();
    let mut cursor = typed_collection.find(filter, find_options).await.unwrap();

    // Iterate over the results of the cursor.
    while let Some(book) = cursor.try_next().await.unwrap() {
        println!("title: {}", book.title);
    }
}

executes and prints

original book:    Book { title: "1984", author: "George Orwell", problem: 21010 }
bson roundtrip:   Book { title: "1984", author: "George Orwell", problem: 21010 }
binary roundtrip: Book { title: "1984", author: "George Orwell", problem: 21010 }
title: 1984

Please note that BSON, unlike JSON, distinguishes between integer and floating-point values; is it possible that the data you're trying to retrieve has this field stored as a float?

from bson-rust.

EliHarper avatar EliHarper commented on July 29, 2024

You're right! I apologize for the oversight.
Thanks so much for getting back to me!

from bson-rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.