$limit with $lte and $gte in mongodbAsk Question

问题:

Is it possible to get a limited number of documents closest to a number in mongodb?

A combination of $lte and $gte allows me to get "all products for which their price is at least 20 but at most 40" - but can I do "get me 10 products with a price above 25 and below 25 which are closest to 25"? I tried this, but sorting on price (up or down), didn't allow me to do this - I can only do this in two separate queries to the database, i.e. one query for $gte with 10 products above sorted with ascending price and one with $lte with 10 products below sorted with descending price. Is there any way to combine this query into one?

An alternative would be "get me 20 products closest to price 25 (regardless whether the price is above or below)" but this again is something I don't think can be done in one query, can it?

标签: sorting
© 2014 TuiCode, Inc.