This question already has answers here:
How to ORDER BY FIELD VALUE in MongoDB
(2 answers)
Closed 6 years ago.
I there a way to do ORDER BY FIELD in MongoDB?
In Mysql there is something like:
SELECT id, name, priority
FROM mytable
ORDER BY FIELD(priority, "core", "board", "other")
Can this be achieved in MongoDB?
Similar query is answered in : How to ORDER BY FIELD VALUE in MongoDB
This uses aggregate function. What I want is without aggregate and something with in find.
Not currently. MongoDB only allows to sort a field by normal old ascending and descending order. You cannot give a custom sort order of priority or anything.
The closest would be to use the aggregation framework to assign values to the field to sort the way you want. Though this will not work well with large queries so I would not recommend it.
Yes MongoDB has "Order By" functionality it is achieved using sort() see below for two primitive examples of how to use:
db.collection.find({}).sort({"fieldName" : 1})
For descending sort use:
db.collection.find({}).sort({"fieldName" : -1})
Related
This question already has answers here:
How to do alphanumeric sort in mongoDB?
(2 answers)
Closed 4 years ago.
I have a table in MongoDB in which I can get the fields in descending order
sorted by system_id
db.job_parameters_mongo.find().sort({system_id : -1})
This is fine but gives me incorrect result as system_id is string field.Is there any way that I can convert system_id to number before ordering in descending order.I saw on this site that there is a way to do this using forEach (how to convert string to numerical values in mongodb) but that uses a function.Isn't there any other way like we have to_number in rdbms?
You can use $toInt available in 4.0 version.
db.job_parameters_mongo.aggregate([
{"$addFields":{"system_id":{"$toInt":"$system_id"}}},
{"$sort":{"system_id":-1}}
])
This question already has answers here:
How does MongoDB sort records when no sort order is specified?
(2 answers)
Closed 6 years ago.
Does default find() implicitly sort by _id?
In other words, are 2 mongo lines listed below equivalent?
db.collection.find().sort( { "_id" : 1 } )
db.collection.find()
The cursors uses the natural order if there is no sort defined.
https://docs.mongodb.com/manual/reference/method/cursor.sort/#return-natural-order
Result Ordering
Unless you specify the sort() method or use the $near operator, MongoDB does not guarantee the order of query results.
Return in Natural Order
The $natural parameter returns items according to their natural order within the database. This ordering is an internal implementation feature, and you should not rely on any particular structure within it.
Most of the time it's the insertion order, but that's not guaranteed.
This question already has answers here:
In MongoDB search in an array and sort by number of matches
(2 answers)
Closed 7 years ago.
I have a mongo collection that has docs similar to the schema below. And I have a ui from which a user could filter on the attributes (using logical or). Since its using OR I want the results to be ordered such that I first get the ones that match the most filters. For example: if i filter on author "auth1" and "tag1" I get back both records but the second record is on top. In this example I just have 2 attr to filter on, but there are about 20.
Do you have any suggestions of the best way of tackling this? I was thinking of writing a map-reduce query to compute the "match rank".
{name: "bookName", tags:["tag1"], authors: [] }
{name: "bookName2", tags:["tag1", "tag2"], authors: ["auth1", "auth2"] }
If I understand your proble, You wants sort the result By authors.length and tags.length.
The problem, in MongoDB (I test on 2.6) with the sort(), it is impossible to sort by two parallels arrays. You can sort by album.length or tags.length but not both.
// sort By max to min authors
db.getCollection('rank').find({}).sort({ authors : -1 });
// sort by max to min tags
db.getCollection('rank').find({}).sort({ tags : -1 });
If you watn sort your result by both of them, you should use the Aggregation Framework. You have a great explanation here : https://stackoverflow.com/a/9040269/1506914
It is possible using Aggregation Framework. Have a look at this question, it's similar to yours.
This question already has answers here:
Order of responses to MongoDB $in query? [duplicate]
(6 answers)
Closed 8 years ago.
Let's say I'm asking MongoDB for a query like this:
"someField" : {
"$in" : [9,3,7,1]
}
Will the returned objects be sorted by the order of the $in array?
IOW, when looking at the results, will I see all documents where {"someField" : 9} listed before the documents where {"someField":3 }, and then the 7's and then the 1's?
If not, any tips on how to get that?
Thanks!
Unfortunately, not only will the order of the $in array not affect the order of the results, but there also doesn't appear to be a built-in way to provide a custom sorting function to the sort function. You will mostly likely have to implement your own sorting function after retrieving the results. If you can tell us the language you're using to query MongoDB, we could probably help you with that part (if needed).
This question already has answers here:
MongoDB - how to query for a nested item inside a collection?
(3 answers)
Closed 9 years ago.
Consider my query to be: {cheese:"Cheddar"} and I have the following collections:
{vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Blue"}, {milk:"Chocolate}, {cheese:"Cheddar"}
How do I make a find that returns me all collections that include cheese:Cheddar?
The result would be {vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Cheddar"} but right now it fives me just {cheese:"Cheddar"}. From what I investigated I only found tokens to work with arrays.
I do NOT know the name of the property is cheese, nor do I know if there are any other ingredients.
I am looking for a way to get documents from a collection, where the query is included in a field, by the names of the properties in the query and the respective values.
Using db.collection.findOne({cheese:"Cheddar"}) you will get as a result only one document, maybe {cheese:"Cheddar"} or maybe {vegetable:"Lettuce", cheese:"Cheddar"}, the first one that MongoDB finds depending on the _id field. If what you want is getting both, you should use db.collection.find({cheese:"Cheddar"}).