首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoID关系查询

MongoID关系查询
EN

Stack Overflow用户
提问于 2011-10-04 18:57:49
回答 2查看 384关注 0票数 0

我有这些模型

代码语言:javascript
复制
class State
  include Mongoid::Document
  field :name    
  embeds_many :cities
end


class City
  include Mongoid::Document    
  field :name
  field :zip_code    
  embedded_in :state    
  references_many :organization_addresses
end

class Organization
  include Mongoid::Document    
  field :name    
  references_many :organization_addresses
end

class OrganizationAddress
  include Mongoid::Document    
  field :address
  field :latitude, :type=>Float
  field :longitude, :type=>Float    
  referenced_in :organization
  referenced_in :city
end

现在,我想为一个组织创建一个地址,并将该地址引用到zip_code为06511的城市。首先,我如何找到嵌入在州中的城市。然后我如何引用它。我写了查询State.where("cities.zip_code"=>"06511").count,它不会返回任何东西,因为城市在数据库中。

如何搜索它,然后将其引用到组织的地址?

EN

回答 2

Stack Overflow用户

发布于 2011-10-04 20:26:08

这里的解决方案是两个将城市的id-s保存在State中,然后您可以找到在该数组中具有特定id-s的州。您可以使用IN、NIN (not in)等关键字在其中实现复杂的逻辑。

我真的推荐你去看看this来了解它。

票数 0
EN

Stack Overflow用户

发布于 2011-10-04 19:25:31

您不能在MongoID中引用嵌入文档。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7646821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档