通过id在Mongoid中查找嵌入的文档
问题描述:
如何通过_id在mongoid中找到嵌入的文档? 我已经Childrens
嵌入式Parent
,我想找到一个通过ID:通过id在Mongoid中查找嵌入的文档
Parent.where('children._id' => BSON::ObjectId(children_id).first
但此函数返回一个包含Parent
给定ID的Children
,而不是Children
本身。
感谢
答
如果你已经有了父 - 只是遍历和寻找孩子阵列中的儿童
children = parent.children.select {|child| child._id == children_id }