[Solved]Access an entry in an object

I am newbie to rust... i am trying to access document from couchdb using the sofa crate; code at the end
When i print the document result.. i get the following result. How do i access the "packet' field in the object?

I tired documents["packet"] and that did not work. Any suggestions?:pray:

DocumentCollection { offset: 0, rows: [DocumentCollectionItem { id: "c15a79c86472797b78f4e540ea00e24b", doc: Document { _id: "c15a79c86472797b78f4e540ea00e24b", _rev: "5-b51fff00e06e0b39e1fbb77d5f27c6d1", doc: Object({"_id": String("c15a79c86472797b78f4e540ea00e24b"), "_rev": String("5-b51fff00e06e0b39e1fbb77d5f27c6d1"), "description": String("alpha"), "packet": Number(5432), "version": Number(2)}) } }], total_rows: 1 }
[Object({"_id": String("c15a79c86472797b78f4e540ea00e24b"), "_rev": String("5-b51fff00e06e0b39e1fbb77d5f27c6d1"), "description": String("alpha"), "packet": Number(5432), "version": Number(2)})]

    let client = Client::new("http://v10-lv2p20397:5984".into()).unwrap();
    let dbw = client.db("log_packet");
    assert!(dbw.is_ok());
    let db = dbw.unwrap();
    let documents_res = db.find(json!({
        "selector": {
            "packet": 5432,
            "version": 2 
        },
        "limit": 1
    }));

    assert!(documents_res.is_ok());
    let documents = documents_res.unwrap();
    println!("{:?}", documents);
    println!("{:?}", documents.get_data());

For those reading, here's a formatted version of the output @bosscar posted:

DocumentCollection {
    offset: 0, 
    rows: [
        DocumentCollectionItem {
            id: "c15a79c86472797b78f4e540ea00e24b", 
            doc: Document { 
                _id: "c15a79c86472797b78f4e540ea00e24b", 
                _rev: "5-b51fff00e06e0b39e1fbb77d5f27c6d1", 
                doc: Object({
                    "_id": String("c15a79c86472797b78f4e540ea00e24b"), 
                    "_rev": String("5-b51fff00e06e0b39e1fbb77d5f27c6d1"), 
                    "description": String("alpha"), 
                    "packet": Number(5432), 
                    "version": Number(2)
                })
            } 
        }
    ], 
    total_rows: 1 
}
[
    Object({
        "_id": String("c15a79c86472797b78f4e540ea00e24b"), 
        "_rev": String("5-b51fff00e06e0b39e1fbb77d5f27c6d1"), 
        "description": String("alpha"), 
        "packet": Number(5432), 
        "version": Number(2)
    })
]

If you have access to the Object that you talked about, which is actually a serde_json::Value, then you will be able to get a value from it using the get function with a string as an index, and then checking if it is a number, and using the as_u64... etc. methods on the Value's doc page

1 Like

Thank you i was able to get the value like this

documents.get_data()[0].get(&"packet").unwrap()
1 Like