gpt4 book ai didi

ruby-on-rails - 如何在 Mongoid 中查询多个 id?

转载 作者:行者123 更新时间:2023-12-03 22:54:08 24 4
gpt4 key购买 nike

如果我有几个 ID,请说 [1,2,3]
是否可以在 Mongoid 中一次查询它们?如:
User.where({ id: [1,2,3]})或类似的东西?

最佳答案

您要查找的底层 MongoDB 查询将使用 $in operator :

The $in operator selects the documents where the value of a field equals any value in the specified array.



在 MongoDB 中,你会说:
db.users.find({ id: { $in: [1,2,3] } })

这直接转化为 Mongoid 为:
User.where(id: { :$in => [1,2,3] })

Mongoid 将大多数(全部?)查询运算符修补为 Symbol所以你通常会说:
User.where(:id.in => [1,2,3])

关于ruby-on-rails - 如何在 Mongoid 中查询多个 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com