0

We are using mongoid gem in our application. We have a set of records with nil values, for example, nil, nil, nil,3,4,1. We want to output like 1,3,4,nil,nil,nil.

We are using pagination which uses Mongoid::Criteria object. Hence we cannot write two queries, then concat as it converts into an array and pagination needs Mongoid::Criteria object.

I tried with two queries with nil and not null, and then used the merge method to combine them, but it resulted in adding these two conditions, and resulted in zero records.

Is there any way using mongoid gem to push nil values to the end and not make nil values sorted in ascending order at the beginning?

Also

pipeline = [{"$match": {"_type": "Category"}},{"$project": {"sort_index": 1,"uri_id": 1, "nlt": { "$ifNull": [ "$sort_index", Time.now]}}},{"$sort": { "nlt": 1}}]

Category.collection.aggregate(pipeline)

This gives Mongo::Collection::View::Aggregation, but I need Mongoid::Criteria for pagination.

If we use partition this will return array object, but we need Mongoid::Criteria object

Shrikanth Hathwar
  • 1,140
  • 4
  • 14
  • 25
  • Possible duplicate of [Is there a way to put all the nil value at the end when sorting with mongodb/mongoid?](https://stackoverflow.com/questions/9201981/is-there-a-way-to-put-all-the-nil-value-at-the-end-when-sorting-with-mongodb-mon) – Cyril Duchon-Doris May 10 '18 at 11:35
  • Here its using partition which will convert to the array. I need Mongoid::Criteria onject. – Shrikanth Hathwar May 10 '18 at 13:52
  • 1
    The Mongoid aggregation pipeline and simple queries (those that return `Mongoid::Criteria`) are two different things. Most likely the pagination gem you are using only supports `Mongoid::Criteria`. You might have to implement something yourself if you want to use pagination on aggregations if it does not exist out there. – Cyril Duchon-Doris May 10 '18 at 16:27

0 Answers0