I face this challenge:
Retrieve documents sorted by field A if field B exists/is not null. Otherwise sort by field C.
In a SQL world, I would do two queries and create a UNION SELECT, but I have no idea how to start with Mongo.
Is map/reduce the correct way to go? Or should I focus on "computed field" and use this one. I am relatively new to MongoDB and I am asking for directions.
Edit: As requested, here some sample data:
Given:
| ID | FieldA | FieldB | FieldC |
|------------|--------|--------|--------|
| Document 1 | 10 | X | 40 |
| Document 2 | 20 | <null> | 50 |
| Document 3 | 30 | Z | 60 |
Expected result (the order) including column with calculation as comment
| ID | FieldA | FieldB | FieldC | "A" if "B" !=<null> else "C" |
|------------|--------|--------|--------|------------------------------|
| Document 1 | 10 | X | 40 | 10 |
| Document 3 | 30 | Z | 60 | 30 |
| Document 2 | 20 | <null> | 50 | 50 |
Thank you, schube