MongoDB $in return in order? [duplicate] - mongodb

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).

Related

Does default find() implicitly sort by _id? [duplicate]

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.

ORDER BY FIELD in mongoDB without using aggregate [duplicate]

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})

Mongo find and rank result [duplicate]

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.

Mongodb - Finding a field value existence in another field [duplicate]

This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 8 years ago.
Is it possible to find all documents in a collection where documents with field (_id) values exist in another field (ex. parentID). Taking in consideration that both fields exist in the documents of the same collection? Thanks
Categories.find({'_id': 'parentID'})
{
_id: 11,
parentID: 1
}
I am using MongoDB 2.6.7
Yes, this is easy to do with the $where operator.
db.Categories.find({'$where':"this._id === this.parent"})
This gives you more flexibility than your regular find syntax but be warned that MongoDB needs to evaluate the Javascript so this is slower than a regular query.

mongodb $in limit [duplicate]

This question already has answers here:
What is the maximum number of parameters passed to $in query in MongoDB?
(4 answers)
Closed 6 years ago.
Was just wondering if there is a limit to Mongodb's $in function?
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
I have a collection of users (BIG) and have a smaller subset of ObjectIds stashed somewhere, and I want to select all users (collections) that are in my ObjectIds.
Thanks
Since there's no limit on the number of items in an array as such, you shouldn't have any problem..
For the case when the array is embedded inside a document, you might want to have a look at these:
http://groups.google.com/group/mongodb-user/browse_thread/thread/4a7caeba972aa998?fwc=1
Filtering content based on words
http://groups.google.com/group/mongodb-user/browse_thread/thread/28ae76e5ad5fcfb5