gpt4 book ai didi

ruby-on-rails-3 - Mongoid `any_of` 可以包括使用外部文档关系吗?

转载 作者:可可西里 更新时间:2023-11-01 10:35:40 28 4
gpt4 key购买 nike

我将 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 $or查询,因此 native MongoDB 将类似于:

db.collection.find(
{ "text" :
{ "$or" :
[ { "account.name" => /#{text}/i }, { "principal.name" => /#{text}/i } ]
}
})

Mongo 查询仅在单个集合上运行,因此要解析 account.nameprincipal.name 字段,它们需要嵌入到文档中,例如

{
text:
{
description: "...",
name: "...",
account: { name: "..." },
principal: { name: "..." }
}
}

关于ruby-on-rails-3 - Mongoid `any_of` 可以包括使用外部文档关系吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6092085/

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