Skip to content

Commit

Permalink
count support
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Mar 15, 2020
1 parent 92ab132 commit abf2686
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const user1 = await users.findOne({ _id: insertId });
// find
const users = await users.find({ username: { $ne: null } });

// count
const count = await users.count({ username: { $ne: null } });

// aggregation
const docs = await users.aggregation([
{ $match: { username: "many" } },
Expand Down
29 changes: 29 additions & 0 deletions src/command/count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::*;
use serde_json::Value;
use util::maybe_json_to_document;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct CountArgs {
db_name: String,
collection_name: String,
filter: Option<Value>,
}

pub fn count(command: Command) -> CoreOp {
let fut = async move {
let client = command.get_client();
let data = command.data;
let args: CountArgs = serde_json::from_slice(data.unwrap().as_ref()).unwrap();
let db_name = args.db_name;
let collection_name = args.collection_name;
let filter = maybe_json_to_document(args.filter);

let database = client.database(&db_name);
let collection = database.collection(&collection_name);

let count = collection.count_documents(filter, None).unwrap();
Ok(util::async_result(&command.args, count))
};
CoreOp::Async(fut.boxed())
}
2 changes: 2 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod aggregation;
mod connect;
mod count;
mod delete;
mod find;
mod indexes;
Expand All @@ -10,6 +11,7 @@ mod update;

pub use aggregation::aggregate;
pub use connect::{connect_with_options, connect_with_uri};
pub use count::count;
pub use delete::delete;
pub use find::find;
pub use indexes::create_indexes;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum CommandType {
Delete,
Aggregate,
Update,
Count,
CreateIndexes,
}

Expand Down Expand Up @@ -103,6 +104,7 @@ fn op_command(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> CoreOp {
CommandType::Update => command::update,
CommandType::Aggregate => command::aggregate,
CommandType::CreateIndexes => command::create_indexes,
CommandType::Count => command::count,
};

executor(Command::new(args, zero_copy))
Expand Down
7 changes: 7 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ test(async function testFind() {
assertEquals(notFound, []);
});

test(async function testCount() {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const count = await users.count({ username: "many" });
assertEquals(count, 2);
});

test(async function testAggregation() {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
Expand Down
17 changes: 17 additions & 0 deletions ts/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ export class Collection {
return doc;
}

public async count(filter?: Object): Promise<number> {
const count = await dispatchAsync(
{
command_type: CommandType.Count,
client_id: this.client.clientId
},
encode(
JSON.stringify({
dbName: this.dbName,
collectionName: this.collectionName,
filter
})
)
);
return count as number;
}

public async findOne(filter?: Object): Promise<any> {
return this._find(filter, { findOne: true });
}
Expand Down
1 change: 1 addition & 0 deletions ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum CommandType {
Delete = "Delete",
Update = "Update",
Aggregate = "Aggregate",
Count = "Count",
CreateIndexes = "CreateIndexes"
}

Expand Down

0 comments on commit abf2686

Please sign in to comment.