notmongo 0.1.7

In-process, bad database with an API similar to MongoDB
Documentation
use bson::{oid::ObjectId, Bson};
use notmongo::*;
use serde_json;
use std::{
    convert::{TryFrom, TryInto},
    path::Path,
    str::FromStr,
};

// fn catch_main() -> Result<(), String> {
//     let mut db = Database::new_from_json_file(Path::new("src/notmongo_cli/test-db.json"))?;
//     println!("{}", db.dump_to_json_string(true)?);

//     let find_query =
//         FindQuery::parse_bson(&bson::doc! {"$or": [{"foo": "bar"}, {"not-here": null}]})?;
//     println!("Find:    {}", find_query);

//     // let update_query = UpdateQuery::parse_bson(&bson::doc! {"$set": {"bar": "baz"}})?;
//     // println!("Update:  {}", update_query);

//     let collection = db.collection_mut("animals");

//     // collection.update_many(&find_query, &update_query, false)?;

//     let results = collection.find(
//         &find_query,
//         &Projection::new_keep_all(),
//         0,
//         0,
//         &Sort::new_unsorted(),
//     );

//     let mut have_result = false;
//     for doc in results {
//         have_result = true;

//         // Display
//         let result_str = Bson::Document(doc).into_relaxed_extjson().to_string();
//         println!("{:?}", result_str);
//     }

//     if !have_result {
//         println!("No matches.");
//     }

//     // Do backups work?
//     db.dump_to_json_file(&Path::new("foo/db-backup-test.json"), true, 3)?;
//     // db.dump_to_json_file(&Path::new("test-db-backup.json"), true, 3)?;

//     Ok(())
// }

const OID_STR_1: &str = "63262de863ff65c8327b1de6";
const OID_STR_2: &str = "63262df763ff65c8327b1de7";

fn id_bson(id: &str) -> Bson {
    bson::Bson::try_from(serde_json::json!({
        "_id": {
            "$oid": id
        }
    }))
    .unwrap()
}

fn id_doc(id: &str) -> bson::Document {
    let mut result = bson::Document::new();
    result.insert("_id", ObjectId::parse_str(id).unwrap());
    result
}

fn catch_main() -> Result<(), String> {
    // Initialize the database
    let mut db = Database::new_empty();
    let collection = db.collection_mut("col1");

    // Insert some data
    collection.insert_one(id_doc(OID_STR_1))?;
    collection.insert_one(id_doc(OID_STR_2))?;

    // Perform a query
    let query = FindQuery::parse_bson(id_bson(OID_STR_1)).unwrap();

    let projection = Projection::new_keep_all();

    let sorting = Sort::new_unsorted();

    let res = collection.find(&query, &projection, 0, 0, &sorting);

    for subres in res {
        println!("{:?}", subres);
    }

    // let find_query = FindQuery::new_all();
    // let update_query = UpdateQuery::parse_bson(&bson::doc! {"$unset": "impliedTagIds"})?;
    // println!("Update:  {}", update_query);

    // collection.update_many(&find_query, &update_query, false)?;

    // db.dump_to_json_file(&Path::new("new-db-out.json"), true, true, 0)?;

    Ok(())
}

fn main() {
    match catch_main() {
        Ok(_) => {}
        Err(e) => {
            println!("ERROR: {}", e);
        }
    }
}