mongoid 查询轨道 3

发布于 2024-10-22 05:45:44 字数 1674 浏览 1 评论 0原文

MongoDB/mongoid 是全新的,并且

在 Rails 控制台上的 ruby​​/rails 上仍然很绿:

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">

返回我所期望的结果,但

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

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

>> t.first 
=> nil

结果与以下内容相同:

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

这是模型:

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

我做错了什么?

更新的示例:

>> 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

我是否需要将created_by明确定义为:type =>细绳 ?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗琴音 2024-10-29 05:45:44

看起来created_by作为字符串存储在文档中,但随后您查询以查找ObjectId。 ObjectId 和字符串不同。

尝试更改

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

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

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"))
千纸鹤 2024-10-29 05:45:44

您必须在 mongoid 中包含此功能,如 docs 中所述:

class Person
  include Mongoid::Document
  include Mongoid::Timestamps
end

You have to include this functionality in mongoid as explained in docs:

class Person
  include Mongoid::Document
  include Mongoid::Timestamps
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文