You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rust读取mongodump生成的.bson文件时类型判断异常

mongodump生成的.bson文件实际类型是什么?

我对mongodump生成的.bson二进制文件十分好奇,最初猜测它是BSON数组Bson::Array(array_of_documents),于是进行了如下操作:

1. 在Cargo.toml中添加依赖

# Cargo.toml

[dependecies]
...
bson = "2.1.0"

2. 编写main.rs代码

// 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");
                }
            }
        }
    }
}

3. 执行命令及结果

$ cargo run
...
Not an array

可以确定这是一个BSON文件,但它并非数组类型,请问该文件实际是什么类型?🤔

内容的提问来源于stack exchange,提问作者ir1keren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 04:31:15