Skip to content

DOCSP-32718: count UE code comments #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions source/code-snippets/usage-examples/count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Count documents in a collection

import { MongoClient } from "mongodb";

// Replace the uri string with your MongoDB deployment's connection string.
// Replace the uri string with your MongoDB deployment's connection string
const uri = "<connection string uri>";

const client = new MongoClient(uri);
Expand All @@ -10,21 +12,20 @@ async function run() {
const database = client.db("sample_mflix");
const movies = database.collection("movies");

// Estimate the total number of documents in the collection
// and print out the count.
/* Print the estimate of the number of documents in the
"movies" collection */
const estimate = await movies.estimatedDocumentCount();
console.log(`Estimated number of documents in the movies collection: ${estimate}`);

// Query for movies from Canada.
/* Print the number of documents in the "movies" collection that
match the specified query */
const query = { countries: "Canada" };

// Find the number of documents that match the specified
// query, (i.e. with "Canada" as a value in the "countries" field)
// and print out the count.
const countCanada = await movies.countDocuments(query);
console.log(`Number of movies from Canada: ${countCanada}`);
} finally {
// Close the connection after the operations complete
await client.close();
}
}
// Run the program and print any thrown exceptions
run().catch(console.dir);