I need the first part of $or
(or equivalent query) to be resolved first and to make sure that the first query is always part of the result.
Must use query, not aggregation.
[
{ "docId": "x1" },
{ "docId": "x2" },
{ "docId": "x3" },
{ "docId": "x4" },
{ "docId": "x5" },
...
{ "docId": "xn" },
]
Query:
{
'$or': [ { docId: 'x434' },{} ],
}
I need x434
to be part of the query result, regardless of all other results.
Expected result:
[
{ docId: 'x434' },
{ docId: 'x12' },
{ docId: 'x1' },
...
]
Return:
[
{ docId: 'xn' },
{ docId: 'xn' },
{ docId: 'xn' },
...
]
Results by x434
is not always returned
I tried $or
and $and
queries but nothing worked. I tried regex
too
{
'$or': [ { docId: 'x434' },{} ],
}
3
Answers
So the solution can only be an aggregation:
Result:
A straightforward solution can use
$facet
:See how it works on the playground example
You can use
$unionWith
. It’s behaviour is similar toUNION ALL
in SQL so you can persist x434 at the start. Remember to exclude x434 in the$unionWith
pipeline to avoid duplicate if neededMongo Playground