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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:58:04+00:00 2026-05-20T17:58:04+00:00

brand new to MongoDB / mongoid and still pretty green on ruby / rails

  • 0

brand new to MongoDB / mongoid and still pretty green on ruby / rails

on the rails console:

t = Task.all(:conditions  => {:title => "task2"})

=> #<Mongoid::Criteria
selector: {:title=>"task2"},
options:  {},
class:    Task,
embedded: false>


>> t.first
=> #<Task _id: 4d7e2cdb73ec3227dd000009, title: "task2", latitude: 23.987904829, longitude: -12.9423875,  created_by: "4d792d6973ec3248e3000001">

returns what I would expect, but

t = Task.where(:created_by => "4d792d6973ec3248e3000001")

=> #<Mongoid::Criteria
selector: {:created_by=>BSON::ObjectId('4d792d6973ec3248e3000001')},
options:  {},
class:    Task,
embedded: false>

>> t.first 
=> nil

same result with:

t = Task.all(:conditions  => {:created_by => "4d792d6973ec3248e3000001"})

here is the model:

class Task
  include Mongoid::Document
  field :title
  field :latitude,                  :type => Float
  field :longitude,                 :type => Float
  field :created_by
end

what am I doing wrong ?

updated example:

>> Task.new(:title => "task8", :created_by => "4d792d6973ec3248e3000001")
=> #<Task _id: 4d81037973ec32cc22000003, latitude: nil, longitude: nil, title: "task8", created_by: "4d792d6973ec3248e3000001">

>> t = Task.all(:conditions  => {:created_by => "4d792d6973ec3248e3000001"})
=> #<Mongoid::Criteria
selector: {:created_by=>BSON::ObjectId('4d792d6973ec3248e3000001')},
options:  {},
class:    Task,
embedded: false>

>> t.first 
=> nil

do I need to specifically define created_by as :type => String ?

  • 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-20T17:58:05+00:00Added an answer on May 20, 2026 at 5:58 pm

    Looks like created_by is stored as a string in your document, but then you query looking for an ObjectId. ObjectId’s and strings aren’t the same.

    Try changing

    >> Task.new(:title => "task8", :created_by => "4d792d6973ec3248e3000001")
    

    to

    >> Task.new(:title => "task8", :created_by => BSON::ObjectId("4d792d6973ec3248e3000001"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand new to Ruby/Rails/RSpec, and hopefully this is a dumb/simple question. I'm currently
I'm getting this error message on all projects (including brand new empty templates) I'm
Am brand new to Ruby looking to get Redmine to run on WinServer08 sp1
I am brand new to AS3 and I am trying to move all of
Brand new at Rails. I had a simple Rails 3 app on OS X
I'm brand new to the Entity Framework and trying to learn all it can
I'm working on a rails 3.1 engine, and as it's a brand new release
I am brand new to ruby, and am starting to build a little shopping
I have a brand new Rails 3.1rc4 project. After generating a scaffold, migrating and
I have created two brand new solutions using the latest VS 2010: C# console

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.