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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:51:57+00:00 2026-05-27T08:51:57+00:00

I use MongoDB in my rails application with Mongoid mapper. But I don’t understand

  • 0

I use MongoDB in my rails application with Mongoid mapper. But I don’t understand finders and criteria of querying. For example in mongoid documentaion in section Finders is query Model.all, but if I use that(for example User.all), console return criteria and not the result:

 => #<Mongoid::Criteria
  selector: {},
  options:  {},
  class:    User,
  embedded: false>

But if I use finder Model.first or Model.last, console return specific result. (User.first return specific user, with its fields, as :email, :username and other). Why Model.all wrote in documentation as finders? And what I need doing if I need get all Users or for example Posts?

  • 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-27T08:51:57+00:00Added an answer on May 27, 2026 at 8:51 am

    You can treat the criteria object as array. Finders return a criteria object because criteria is chainable in mongoid. That is, you can do something like:

    users = User.where(:activated => true)
    users = users.where(:created_at.gte => Time.now - 1.week) unless params[:recent].blank?
    users = users.where(:gender => params[:gender].downcase) if %w[m f].include?(params[:gender].downcase
    

    Anytime you use any methods which are not defined in Criteria, mongoid will actually run the query and fetch the results and treat them as array. If you specifically want the results to be returned as array, you can always call User.all.to_a. But keep in mind that following two are mostly equivalent:

    User.all.each {|u| puts u.id}
    User.all.to_a.each {|u| puts u.id}
    

    But there is one issue with the later, it will fetch all the documents in memory once and can lead to too much of memory consumption. However, first one uses Mongodb cursors to fullest and only loads documents yielded by cursor, means controlled memory usage.

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

Sidebar

Related Questions

I'm in the process of converting my Rails app to use mongodb through mongoid.
We decided to use mongodb for some web application (instead of mysql) but want
I've been trying to use Mongoid with Rails on Ubuntu. I have installed mongoDB
i use rails and mongodb (mongoid gem). i need to create a select form
I'd like to use the addToSet method of MongoDB, but Mongoid doesn't currently support
I'm migrating an existing Rails app to use MongoDB (with Mongoid), and I'm having
I have a good reason to use MongoDB for part of my app. But
Is it a good idea to use MongoDB in .NET desktop application?
I recently played with MongoDB in Rails using Mongoid . I like the ability
I use mongomapper with mongodb for rails models. In the mongodb I have this

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.