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