mongoid 查找 VS 哪里

发布于 2024-11-02 03:42:08 字数 986 浏览 4 评论 0原文

当我通过 ID 搜索时,我似乎只在 1 个特定模型上遇到此问题,

>> 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 很好。有什么想法吗?

堆栈的其余部分是...

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

i seem to only be having this issue with 1 particular model when i am searching by ID

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

the strange thing is that with other models, i can use find just fine. any ideas?

the rest of the stack is...

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

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

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

发布评论

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

评论(1

奶茶白久 2024-11-09 03:42:08

通常问题是相反的。失败的地方和有效的地方。

这是由于在查询之前没有将 id 转换为 BSON::ObjectId 造成的。

通常你必须这样做

Cart.where(:_id => BSON::ObjectId('4dae5902e1607c232c000009')).first

这让我相信你的 id 存储为字符串而不是 BSON:ObjectId 并且会解释为什么 find 失败(它正在搜索 BSON::ObjectId 而不是字符串)

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

希望有帮助

Normally the issue is the other way around. With the where failing and the find working.

That is cause by where not casting the id into BSON::ObjectId before the query.

Usually you would have to do this

Cart.where(:_id => BSON::ObjectId('4dae5902e1607c232c000009')).first

This leads me to believe that your ids are stored as strings and not BSON:ObjectId and would explain why find fails ( it is searching for a BSON::ObjectId not a string)

Could also explain why it is only one model as it depends entirely on how the objects are stored.

Hope that helps

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