Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:24:21+00:00 2026-06-18T08:24:21+00:00

I’m storing day numbers 0-6 in an array on a document. I want the

  • 0

I’m storing day numbers 0-6 in an array on a document.

I want the documents to always be in the correct order but sunday(0) is making that troublesome since it will be before monday(1). How could I make this ordering in another way?

default_scope asc(:days)

Update:

So when trying to implement a custom serialization I ran into a problem:

class Schedule
  include Mongoid::Document
  field :days, type: Days  
  default_scope order_by([[:days, :asc]])
end  

class Days
  include Mongoid::Fields::Serializable

  # Convert Ruby wday format, sunday(0) - saturday(6).
  def deserialize(days)
    days.map { |day| (day + 1)%7 }
  end

  # Convert to MongoDB friendly format, monday(0) - sunday(6).
  def serialize(days)
    days.map { |day| (day + 6)%7 }
  end
end

So far so good, the custom field serialization works as expected:

s2 = Schedule.create(days: [0])
s.days 
# Deserialized value => [0]
s.read_attribute :days
# Stored value => [6]

However, Mongoid seems to sort on the deserialized object?

s2 = Schedule.create(days: [1,2,3,4,5])
s2.days
# Deserialized value => [1, 2, 3, 4, 5]
s2.read_attribute :days
# Stored value => [0, 1, 2, 3, 4] 

Schedule.all
# Sorted as => [s1, s2]
# Expected sort order => [s2, s1]

Confirmed by removing the deserialization, that will return the documents in the correct order. It might be worth noting that this is an embedded document, I guess maybe the ordering is done in a different way.

Update 2:
I ended up creating a second field, sortable_days, which I used the serializer on, but not the deserializer, and then sorted on that field instead.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:24:22+00:00Added an answer on June 18, 2026 at 8:24 am

    MongoDB only allows sorting on a field for forward/reverse values. By using 0 for sunday, there’s no way to preserve the ordering for 1-6 but put 0 at the end. In Postgres or other SQL servers you could use a custom sort function to achieve this, but there is no analogue in MongoDB.

    You will need to change your value encoding for this. You could create a denormalize version of this value that has the transformed (0 => 7) value for this field and sort on this field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I want to construct a data frame in an Rcpp function, but when I
I am confused How to use looping for Json response Array in another Array.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.