I need some assistance developing a search query to give a button in my Electron application some functionality.
This is how far I have gotten:
module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
// write a query that will follow sort, offset, limit options only
// do not worry about criteria yet
Artist.sort()
.find()
.offset()
.limit();
};
I am skipping criteria
for now so feel free to ignore that. The user should be able to sort by artist name, age and albums released. The sortProperty
will go in ascending fashion so I know I need to sort with a value of 1
.
The real challenge behind the sorting is that I need to specify different sortProperties
but only one at any given time.
I want to be able to pass in a different sortProperty
depending on what the user has selected. I would like to have a key that is whatever the sortProperty
is.
So if sortProperty
is equal to age, I need to ensure I have a statement that says age.
It seems you already have all the required parts to do the query. You just need to create the sort
selector based on your sortProperty
. This will be an object with a key equal to the value held by sortProperty
for example age
. The result will look like this
module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
return Artist
.find()
.sort({ [sortProperty]: 1 })
.skip(offset)
.limit(limit);
};
To illustrate the dynamic key assignation, here's a snippet
const sortProperty = 'age'; const selector = { [sortProperty]: 1 }; console.log(selector);
So the above answer is the best solution as it is ES6 and more elegant. I wanted to share the ES5 old school way that I learned after I got this answer and studied it more in-depth.
In the ES5 way I learned that you can define an object like so:
module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
// write a query that will follow sort, offset, limit options only
// do not worry about criteria yet
const sortOrder = {};
Artist.find({})
.sort()
.skip(offset)
.limit(limit);
};
I made it an empty object and then look at it and add sortProperty
equals 1
like so:
module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
// write a query that will follow sort, offset, limit options only
// do not worry about criteria yet
const sortOrder = {};
sortOrder[sortProperty] = 1;
Artist.find({})
.sort()
.skip(offset)
.limit(limit);
};
And then I can pass in the sortOrder
like so:
module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
// write a query that will follow sort, offset, limit options only
// do not worry about criteria yet
const sortOrder = {};
sortOrder[sortProperty] = 1;
Artist.find({})
.sort(sortOrder)
.skip(offset)
.limit(limit);
};
Okay, so that is the ES5 approach if anyone was interested in learning about it, but as you can see above the ES6 solution looks better.
The whole problem with creating an object and simultaneously adding a property to it is that its not well supported in ES5, we have to first declare an object, then add a property to it and set that property equal to 1.
The square brackets mean look at the sortProperty
variable:
sortOrder[sortProperty] = 1;
which is a string and I am trying to add on a sortOrder
property of name
to this object and set it equal to 1
and so the end result if you ran this in a Chrome console or in code snippet is { name: 1 }
.
The learning curve for me and what the selected answer helped me learn was ES6 interpolated properties or keys. This thing:
.sort({ [sortProperty]: 1 })
What is happening here is at runtime look at the sortProperty
variable and whatever its equal to, add that property to this object and give it a value of 1
.
So to be clear, yes the original answer given was the elegant solution I went with:
module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
// write a query that will follow sort, offset, limit options only
// do not worry about criteria yet
Artist.find()
.sort({ [sortProperty]: 1 })
.skip(offset)
.limit(limit);
};
I was just sharing what I learned along the way in studying and implementing it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.