I was very curious about .bson binary file produced by mongodump.
At first, my guessing was BSON Array Bson::Array(array_of_documents)
Then:
#Cargo.toml
[dependecies]
...
bson = "2.1.0"
//main.rs
fn main()
{
let mut path=std::env::current_dir().unwrap();
path.push("backup/transactions.bson");
if path.exists() {
match std::fs::File::open(&path)
{
Err(err)=>panic!("Failed to open '{}'",path.to_str().unwrap(),err),
Ok(file)=>{
if let Ok(bson_file)=bson::Bson::from_reader(&file) {
if let Some(bson_array)=bson_file.as_array() {
for bson_data in bson_array {
println!("BSON Data: {:?}",&bson_data);
}
} else {
println!("Not an array");
}
} else {
println!("Not a BSON file");
}
}
}
}
}
Then I ran:
$ cargo run
...
Not an array
Clearly a BSON file, but not an array! What was it then?
Then I thought.. what if it was a Document
Bson::Document(bson_data)?Those keys are my Transaction data! But it only retrieves one document and prints all keys of the document. Where are the rest of those documents? I have more than 50 documents dumped in .bson file. Then I suspected something, what if.. a .bson file is consisted of a series of documents being placed next to the other.
Yes..my guessing was right. A .bson file is a series of
Bson::Document(data)being placed next to the other