mongodb, sort documents ascending, with undefined bottom - mongodb

I have documents collection where some documents have price, some don't.
I want to sort them ascending, with null(empty) last.
Googled some solutions:
Do two queries: first with price not null and price sorting, and second with price is null, and then combine data
Project new field with aggregate for sorting. Make it equal to price for documents with price and huge number where price is null. Then sort by this new field.
Is there any better/clean way to do it?

Related

Mongo complex multi sort

I would like to know if mongo lets you do complex multi sorting and how to do it.
Imagine that you have Products with two bollean (0/1) fields "Sale" & "Focus" and a price (double).
I would like to return all products but order then by:
1- Products on Focus and on Sale
2- Products on Focus
3- Products on Sale
and inside each one of this three groups they should be ordered by price desc
Is this even doable in a single query sort?

Order of results for `sort` using mongoose

If I have two equal values for a field. What would be the order of results for sort on that field? Random or ordered by insertion date?
If two documents have equal values for the field you're sorting on, then MongoDB will return the results in the order they are found on disk (ie Natural order)
from MongoDB Documentation :
natural order:
The order in which the database refers to documents on
disk. This is the default sort order. See $natural and Return in
Natural Order.
This may coincide with insertion date in some case, but not all of the time (especially when you perform insertion/deletion on your collection), so you should assume that this is random ordering

Query by position in mongodb collection

I need to fetch the document in a mongodb collection using its position. I know the position of the document inside the collection exactly but could not figure out a way to pull those documents from collection. Is there any way to achieve this?
db.daily.find({'_id': {'$in': 0,5,8}})
This is what i tried but _id is not inserted as 1,2,3... but it has some random num Eg:57d8fd62f2a9d913ba0d006d. Thanks in advance.
You can use skip and limit to query based on the position in the natural order
db.collection.find().skip(10).limit(1) // get 10th document in natural order
As the natural order link points out, the document order need not match the order that documents are inserted (with an exception for capped collections). If you use the default ObjectId as the _id field for your documents you can sort by _id to order based on insertion in the collection (up to the resolution of the timestamp in the ObjectId)
db.collection.find().sort([("_id",1)]).skip(10).limit(1) // get 10th document in inserted order
You may also consider using your own _id or adding a field to be able to sort on in order to query based on the position you define.

store documents in descending order(mongodb-insert)

I am trying to add and retrieve documents from collection.I went through mongodb manual and didn't find ways to add documents in descending order by date. Is it possible to add documents to collection in descending order by date while inserting the document as i don't want to query and return the top 50 documents.
TIA.
MongoDB does not guarantee the retrieval order of documents. Even if you insert them in descending order, there is no guarantee the records will be returned in this order. As suggested by BatScream you could add an descending index on your date field and the order your result when querying the data.

MongoDB: Count of matching nested array elements

I've got a simple parent child object stored as a document in MongoDB. Something simplistic like Order/OrderItems. (Order has an array of OrderItem)
What I'd like to do is query for a count of Order Items where they meet a set of criteria.
Example: In Order "999" find out how many order items had a quantity of 3.
db.collection.find( {OrderId:999, "OrderItems.QuantityOrdered":3} ).count();
The way this query works is it returns "1" because if it matches at least one OrderItem inside the array it will return the count of Orders matched.
How can I query for how many "OrderItems" matched?:
There's no direct way of return this sort of count with an embedded document. With this sort of ad-hoc count, your best bet is to return the document and do the count from the application.
If you want to perform this kind of count for a large number of orders, you could use map-reduce, which will output the results to a new collection.