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

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.

Related

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

Do MongoDB returns query results always in same order? [duplicate]

This question already has answers here:
How does MongoDB sort records when no sort order is specified?
(2 answers)
Closed 6 years ago.
According to this answer,
The results are returned in the order they are found, which may
coincide with insertion order (but isn't guaranteed to be)
I am concerned with order of documents in result. Doesnt matter how they are stored and indexed.
For example,
If I issue a query and I got result in order
[id1, id3, id6, id2]
Again I re-issued the same query then order may change or not?
It is fine if I get result like below,
[id1, idx, id6, id2] //Because I inserted idx and deleted id3
I expect id1 should always appear before id6, and id6 before id2.
I am using mongodb in sharded environment.
Do MongoDB returns query results always in same order?
The short answer is: No.
I presume we're talking about cases when explicit sort() method is not used. In this case, you can expect for the same results order only if you are querying a capped collection.

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 return in order? [duplicate]

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

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