检索Mongoid中的ID数组 [英] Retrieving array of ids in Mongoid
本文介绍了检索Mongoid中的ID数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Mongoid
中检索IDs
的数组?
arr=["id1","id2"]
User.where(:id=>arr)
如果要检索其他属性,则可以轻松完成此操作
You can do this easily if you are retrieving another attribute
User.where(:nickname.in=>["kk","ll"])
但是我想知道如何在mongoid中执行此操作->这应该是一个非常简单且常见的操作
But I am wondering how to do this in mongoid -> this should be a very simple and common operation
推荐答案
请记住,该ID存储为:_id
而不是:id
.有一个id
辅助方法,但是在执行查询时,应使用:_id
:
Remember that the ID is stored as :_id
and not :id
. There is an id
helper method, but when you do queries, you should use :_id
:
User.where(:_id.in => arr)
通常,我发现获取ID列表来执行复杂的查询很有用,所以我做了类似的事情:
Often I find it useful to get a list of ids to do complex queries, so I do something like:
user_ids = User.only(:_id).where(:foo => :bar).distinct(:_id)
Post.where(:user_id.in => user_ids)
这篇关于检索Mongoid中的ID数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文