score:0
Try this one:
db.collection.aggregate([
{
$addFields: {
sortQuantity: { $cond: [ { $lte: ["$quantity", 0 ] }, 0, 1 ] }
}
},
{
$sort: {
sortQuantity: 1,
sort_order: -1
}
},
{ $unset: "sortQuantity" }
])
score:0
You can use
$facet
to categorize incoming data intoquantity>0
andquantity <=0
$sort
to sort as you expected$concatArray
to concat both array into one array$unwind
to deconstruct the array$replaceRoot
to make the deconstructed variable as root
here is the ocde
db.collection.aggregate([
{
"$facet": {
"lteZero": [
{
$match: {
$expr: {
$lte: [ "$quantity", 0 ]
}
}
},
{ $sort: { sort_order: -1, quantity: 1 } }
],
"gtZero": [
{
$match: {
$expr: {
$gt: [ "$quantity", 0 ]
}
}
},
{ $sort: { sort_order: -1, quantity: 1 } }
]
}
},
{
"$project": {
combined: {
"$concatArrays": [ "$gtZero", "$lteZero" ]
}
}
},
{ "$unwind": "$combined" },
{ "$replaceRoot": { "newRoot": "$combined" } }
])
working Mongo playground
score:0
It is simple and easy, you may use a cursor.sort() function to order the collection:
db.collection.find().sort( { quantity: -1 } )
These examples return all documents in the collection named collection sorted by the quantity field in descending order. Specify a value to $orderby of negative one (e.g. -1, as above) to sort in descending order or a positive value (e.g. 1) to sort in ascending order.
For more information please visit: https://docs.mongodb.com/manual/reference/method/cursor.sort/
More questions
- MongoDB sort with conditions
- Accessing nested MySQL Select with multiple conditions in PHP.
- Complex statement in MongoDB AND & OR conditions
- DQL with conditions in SUM
- MySQL Select - Sort by best match with multiple OR operators
- group_concat in mysql with "case when " conditions
- UPDATE records with conditions in mysql
- Compare with a value and sort in mysql
- left join table with two conditions
- Convert rows to columns with some conditions in sql
- MySQL Get rows that do not match with certain conditions
- MongoDB loads of daily data (or stick with mysql?)
- htaccess redirectmatch with conditions and need to query the database
- SQL query pulling hardcoded records and amounts all with the same WHERE conditions
- Idea to sort (My)SQL results with C#
- SQL sort by count with condition, show history
- Select records with 2 type of sort
- MySQL SELECT query with conditions
- How do I Sort a varchar with numbers and letters without a specific format?
- Why does MySQL sort correctly but SQLite doesn't when field is marked with quotation marks?
- MySQL:How to sort date with RFC112 format?
- grails findAll with conditions or executeQuery
- counting with conditions in mysql
- I need to search with multi variable and sort
- How do I sort multiple options when more than one checkbox is checked with jQuery/AJAX and PHP
- Can I pull data from a .xls file with PHP, sort and call the data without MySQL?
- Problems while trying to make a query with variables in the conditions (stored procedure)
- Mysql: How to Query 1 table with 2 different conditions and display result in separate columns
- Mysql order by multiple columns with conditions
- Laravel use sum with a few conditions
More questions with similar tag
- mysql dependent subquery stops working when using a constant
- InnoDB purge worker in mariadb processlist
- How can I fetch data on codeigniter using current date?
- SQL query for movie database
- MySQL: Multiple select from some servers in one query?
- Group by Multi Language Field
- join mysql tables and get multiple rows
- Fastest and most efficient way to fetch a record from Remote MySQL to iPhone
- Split separated data by comma into multiple rows
- Get reports only last trynumber report of user on exercise _id
- I got error code 1442 for creating TRIGGER in MySQL and another syntax error for my other trigger? How can I fix it?
- Conditionally updating the columns PHP/MySQL
- Optimizing Update Query with compound index
- PHP Form that updates multiple form element to different columns
- Parse error in php validation
- problem in mysql query with join and limit the time
- Concatenation doesn't work in phpmyadmin XAMPP
- Why does having two LEFT JOINS result in additional values?
- MySQL query with array values
- Mysql find customers that bought X but not Y
- How do I select top ranker by date in MySQL
- Convert NoSQL like syntax to MySQL syntax
- Mysql transactions issue
- Primary / foreign key; joining multiple tables using subqueries
- SQL Query fetching wrong rows
- Can I insert UTF8 encoded characters into a Latin-1 table if I know only Latin-1 characters will be used?
- How to get data from database with kohana?
- MySQL Insert if select is null to avoid bidirectional duplicates
- Why does a Mysql table size affects runtime with LIMIT command
- Mysql maximum rows in a variable timeframe