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 1063861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:50:52+00:00 2026-05-16T18:50:52+00:00

I must have done it in a wrong way: the records printed out are

  • 0

I must have done it in a wrong way: the records printed out are out of order, even though they are inserted into db one at a time. Here is the code:

get '/' do
  db = Mongo::Connection.new("localhost", 27017).db("testdb")
  @notes = db.collection('notes')
  @notelist = Set.new()
  @notes.find().each{|record| @notelist.add(record)}
  erb :list
end

post '/addnote' do
  db = Mongo::Connection.new("localhost", 27017).db("testdb")
  col1 = db.collection('notes')
  col1.insert(
  {
    "guestname" => "#{params[:post][:guestname]}",
    "note"      => "#{params[:post][:note]}",
    "datetime"  => Time.now.strftime("%I:%M %p %d-%b-%Y")
  })
redirect '/'
end

And here is the erb template:

<p><%= @notelist.size() %> notes entered by guests:</p>
<ul>
  <% @notelist.each do |record| %>
    <li><font color='blue'><%= record['guestname'].to_s() + 
    "</font> at <i>" + record['datetime'].to_s() +"</i> wrote: " + 
    record['note'].to_s() %></li>
  <% end %>
</ul>

I am trying to get all records out of the db in an order of datetime, how can I achieve that?

Thanks a lot in advance.

Updated info:

On second thought, I change the data type from time to unix epoch, so sorting them would be better and easier.

  • 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-05-16T18:50:53+00:00Added an answer on May 16, 2026 at 6:50 pm

    A few notes here:

    You can get records in order by adding a sort:

    @notes.find({}, :sort => 'datetime')
    

    You do not need to iterate before entering your template. The ‘find’ method returns a cursor, which itself is iterable. So

    @notelist = Set.new()
    @notes.find().each{|record| @notelist.add(record)}
    

    Should read

    @notelist = @notes.find({}, :sort => 'datetime')
    

    It’s very inefficient to establish a new connection on each request. You should establish the connection on a configure block, and cache the database there:

    configure do
      DB = Mongo::Connection.new.db("testdb")
    end
    

    Then just use the reference to DB in your requests.

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

Sidebar

Related Questions

I must have done something wrong. I have a C# project in Visual Studio
I must have pressed on the wrong button at some point, and now when
You must have noticed one link in yahoo.com, msn.com or other popular websites named
They must have hidden the bug database somewhere. I could not find it with
I must have a misunderstanding of the stack, or how functions are called, the
This must have an obvious solution, but I'm stumped. We are developing an application
I must have overlooked something completely obvious?? but my button displays its image and
I must have a little design in javascript. I have a menu with 5
you must have seen this piece of code somewhere else too,but obviously there's no
This must have been answered previously, but my Google powers are off today and

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.