【问题标题】:Special where on embedded relations - Laravel MongoDB嵌入式关系的特殊位置 - Laravel MongoDB
【发布时间】:2018-11-27 11:04:39
【问题描述】:

我有以下 用户 集合:

{
  "type": "provider",
  "name": "user name",
  "username": "username",
  "password": "$2y$10$D3z0tLwOwB0tqPEnl63VuexOwqcR75QkVILemB1.TEsAJlk6Ixwim",
  "specialties": [
      "specialty 1",
      "specialty 2"
  ]
}

还有一个特色菜系列:

{
  "_id": "5b26103b2df243228c0003ea",
  "title": "specialty 1",
  "description": "specialti 1 desc",
},{
  "_id": "5b26103b2df243228c0003ea",
  "title": "specialty 2",
  "description": "specialti 2 desc",
},

它们之间的关系是嵌入式的,这是我在用户模型中的关系,

public function specialties()
{
    return $this->embedsMany(Specialty::class, 'specialties', 'title');
}

我想按专业过滤users。例如,如果过滤后的专业为“专业 1”,则应返回上述 JSON 用户对象。

我知道非嵌入式集合,但我的数据保存在我的数据库中,我不能不更改架构。

有其他解决方案吗?

【问题讨论】:

    标签: database mongodb laravel-5


    【解决方案1】:

    解决方案在文档中,MongoDB specific operators

    就我而言,答案是,

    $providers = User::where('specialties', 'all', ['specialty 1'])->with('s_specialties')->get();
    

    此代码模拟 MongoDB 中的 $in 运算符。 More about operators.

    【讨论】:

      猜你喜欢
      • 2022-11-04
      • 2016-12-14
      • 2011-04-18
      • 2011-01-19
      • 1970-01-01
      • 2015-03-10
      • 2014-10-11
      • 1970-01-01
      • 2022-12-29
      相关资源
      最近更新 更多