我将MongoDB与Mongoid一起使用,并试图在考虑权重等之前先进行一个基本的搜索作为占位符。any_of
方法似乎是在查找我的嵌入文档,但不是那些通过关系链接的文档。有人知道any_of
是否可以包含与数据库中其他文档的关系吗?如果可以,语法是什么?
belongs_to :principal #owner
belongs_to :account #owner
scope :search, ->(text) { any_of(
{:description => /#{text}/i},
{:name => /#{text}/i},
{"entries.title" => /#{text}/i},
{"entries.description" => /#{text}/i},
{:tags => /#{text}/i},
{"account.name" => /#{text}/i}, # Not finding by account name - because account isn't embedded?
{"principal.name" => /#{text}/i} # Not finding by principal name - because not embedded?
)}
不,any_of相当于MongoDB$或查询,因此本地MongoDB应该类似于:
db.collection.find(
{ "text" :
{ "$or" :
[ { "account.name" => /#{text}/i }, { "principal.name" => /#{text}/i } ]
}
})
Mongo查询只在单个集合上运行,因此要解析account.name
和principal.name
字段,需要将它们嵌入文档中,例如
{
text:
{
description: "...",
name: "...",
account: { name: "..." },
principal: { name: "..." }
}
}