I have this collection, the objects in them look like:
{
name: 'Apple',
listOrder: {
default: 1,
wallmart: 1
}
color: 'Red'
},
{
name: 'Orange',
listOrder: {
default: 2,
wallmart: 3
}
color: 'Orange'
},
{
name: 'Lemon',
listOrder: {
default: 3,
wallmart: 2
}
color: 'Yellow'
}
I have 3 fruit stands that need to get them back in the right order. I have tried several different ways to do this, and I feel that I'm on the right track with:
function getFruit(storeName){
var override = {check:{}, sort:{}}
override.check[`listOrder.${store}`] = {$exists:1}
override.sort[`order.${store}`] = 1
var sort = {sort:{$cond:{if:override.check, then:override.sort, else:{'listOrder.default':1}}}}
return FruitsCollection.find({}, sort).fetch()
}
It tells me that $cond is not a supported sort key. I am wondering 3 things:
- Would it be better optimized to sort them on the client? I'm trying to keep mobile in mind, so, sorting like this makes the page take several seconds to load as I have faaar more than just these 3 fruits.
- Is there a better sort query that I can use? I'm a bit new to Mongo.
- Is this just a generally bad idea?
This is currently being used on the client, I can move it to the server, however it also tells me I'm using a bad sort specification.