首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongoid find VS where

mongoid find VS where
EN

Stack Overflow用户
提问于 2011-04-20 12:23:28
回答 1查看 3.9K关注 0票数 11

当我按ID搜索时,我似乎只有一个特定的型号有这个问题

代码语言:javascript
复制
>> Cart.where(:_id => '4dae5902e1607c232c000009').first
=> #<Cart _id: 4dae5902e1607c232c000009, _id: BSON::ObjectId('4dae5902e1607c232c000009'), _type: nil>
>> Cart.find('4dae5902e1607c232c000009')
Mongoid::Errors::DocumentNotFound: Document not found for class Cart with id(s) 4dae5902e1607c232c000009.

奇怪的是,对于其他模型,我可以使用find很好。有什么想法吗?

栈的其余部分是..。

代码语言:javascript
复制
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:192:in `execute_or_raise'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:190:in `tap'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:190:in `execute_or_raise'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:106:in `find'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/finders.rb:67:in `find'
from (irb):37
EN

回答 1

Stack Overflow用户

发布于 2011-04-20 21:15:50

通常情况下,问题是相反的。where失败而find正常工作。

这是因为在查询之前没有将id转换为BSON::ObjectId。

通常你必须这样做

代码语言:javascript
复制
Cart.where(:_id => BSON::ObjectId('4dae5902e1607c232c000009')).first

这使我相信您的is存储为字符串,而不是BSON:ObjectId,这可以解释为什么find失败(它搜索的是BSON::ObjectId,而不是字符串)

还可以解释为什么它只是一个模型,因为它完全取决于对象的存储方式。

希望这能有所帮助

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5725469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档