Closed
Description
Versions/Environment
- What version of Rust are you using? 1.70
- What operating system are you using? macOS Ventura 13.4.1
- What versions of the driver and its dependencies are you using?
- https://github.com/rust-lang/crates.io-index#bson@2.6.1
- https://github.com/rust-lang/crates.io-index#mongodb@2.6.1
- What version of MongoDB are you using? 5.0.12
- What is your MongoDB topology standalone
Describe the bug
We're actually trying to read a file from a GridFsBucket in Java which was created in Rust.
Java claims that it cannot cast a Long value into an Integer.
Im not sure why this happens but in my case on the Upload side in Rust the "chunkSize" in "fs.files" and "n" in "fs.chunks" has the type int64.
It seems like the driver is implemented with a u32 ?!
https://github.com/mongodb/mongo-rust-driver/blob/v2.6.1/src/gridfs.rs#L55
https://github.com/mongodb/mongo-rust-driver/blob/v2.6.1/src/gridfs.rs#L34
Currently we use the Java driver version:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.6.1</version>
<scope>compile</scope>
</dependency>
A File upload on the java side stores chunkSize and n as i32.
To Reproduce
Steps to reproduce the behavior:
- Create File via GridFsBucket in Rust
- Read File via GridFsBucket in Java
For now the following Code fixes my Problem but it would be nice to remove this Workaround:
async fn fix_mongodb_types(db: &DB, oid: &Bson) -> anyhow::Result<()> {
let mut opts = FindOneOptions::default();
opts.projection = Some(doc! { "chunkSize": 1});
let file = db.client().collection::<DocChunkSize>("fs.files").find_one(doc! { "_id": &oid }, Some(opts)).await?.unwrap();
db.client().collection::<Document>("fs.files").update_one(
doc! { "_id": oid },
doc! {
"$set": {
"chunkSize": Bson::Int32(file.chunk_size as i32)
}
}, None).await?;
let mut opts = FindOptions::default();
opts.projection = Some(doc! { "_id": 1, "n": 1 });
let mut chunks = db.client().collection::<DocN>("fs.chunks").find(doc! { "files_id": &oid }, None).await?;
while let Some(ch) = chunks.next().await {
if let Ok(ch) = ch {
db.client().collection::<Document>("fs.chunks").update_one(
doc! { "_id": &ch.id },
doc! {
"$set": {
"n": Bson::Int32(ch.n as i32)
}
}, None).await?;
}
}
Ok(())
}