Skip to content
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

Mongo API by Joyce #514

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Mongo API by Joyce #514

wants to merge 6 commits into from

Conversation

JoyceKuode
Copy link

Comment on lines +75 to +97
const query = {};

// Add artist filter to the query if provided
if (artist) query.artistName = new RegExp(artist, "i"); // Case-insensitive regex search

// Add genre filter to the query if provided
if (genre) query.genre = new RegExp(genre, "i");

// Add BMP filter to query if provided
if (bpm) query.bpm = Number(bpm);

// Add popularity filter to query if provided
if (popularity) query.popularity = { $gte: Number(popularity) }; // Greater than or equal

// Initialize sorting object
let sortBy = {};
if (sort) {
const sortOrder = order === "desc" ? -1 : 1; // Set order (-1 for Descending, 1 for ascending)
sortBy[sort] = sortOrder; // Dynamically set sort field and order
}

// Query database using query object and sort options
const tracks = await Music.find(query).sort(sortBy);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done query-building ⭐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants