Skip to content

DOCSP-32718: Compound comments #771

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 6 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
9 changes: 9 additions & 0 deletions source/code-snippets/indexes/compound.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ const { MongoClient } = require("mongodb");
const uri =
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";

// Create a new client and connect to MongoDB
const client = new MongoClient(uri);

async function run() {
try {
// begin-idx
// Connect to the "sample_mflix" database
const database = client.db("sample_mflix");
// Access the database's "movies" collection
const movies = database.collection("movies");

// Create an ascending index on the "type" and "genre" fields
Expand All @@ -20,18 +23,24 @@ async function run() {
// end-idx

// begin-query
// Define a query to find movies in the "Drama" genre
const query = { type: "movie", genre: "Drama" };
// Define sorting criteria for the query results
const sort = { type: 1, genre: 1 };
// Include only the type and genre fields in the query results
const projection = { _id: 0, type: 1, genre: 1 };

// Execute the query using the defined criteria and projection
const cursor = movies
.find(query)
.sort(sort)
.project(projection);
// end-query

} finally {
// Close the MongoDB client connection
await client.close();
}
}
// Run the function and handle any errors
run().catch(console.dir);