How to ORDER BY FIELD VALUE in MongoDB -
in mysql use field()
function in order by
clause:
order field(id, '1', '6', '3', ...);
how 1 same results in mongodb? tried following:
.find(...).sort({id: [1, 6, 3]})
this did not work
so record:
given array [1,6,3]
want in query this:
db.collection.aggregate([ { "$project": { "weight": { "$cond": [ { "$eq": ["_id": 1] }, 3, { "$cond": [ { "$eq": ["_id": 6] }, 2, { "$cond": [ { "$eq": ["_id": 3] }, 1, 0 ]}, ]}, ]} }}, { "$sort": { "weight": -1 } } ])
and gives specific "weights" order of "array" of inputs "project" weights upon results.
Comments
Post a Comment