【问题标题】:Laravel mongodb search data in objectLaravel mongodb 在对象中搜索数据
【发布时间】:2021-09-09 07:23:30
【问题描述】:

我正在构建一个与 MongoDB 连接的 Laravel 应用程序。现在我从数据库中查询数据时遇到了一个问题。

table name: Employee    
[
  {
    "_id": 1,
    "name": "user 1",
    "managers": [
      {
        "id": 321,
        "user_id": 1,
        "ack_groups: [
          ["2","3", "4"],
          ["2", "5", "6"]
        ]
      },
      ......
    ] 
  },
  {
    "_id": 2,
    "name": "user 2",
    "managers": [
      {
        "id": 3213,
        "user_id": 2,
        "ack_groups: [
          ["6","7", "8"],
          ["2", "5", "6"]
        ]
      },
      ......
    ] 
  },
  {
    "_id": 3,
    "name": "user 3",
    "managers": [
      {
        "id": 321,
        "user_id": 3,
        "ack_groups: [
          ["8","9", "14"],
          ["12", "15", "16"]
        ]
      },
      ......
    ] 
  },
]

我正在尝试在某些条件下从 Employee 表中查询员工数据列表。条件是我想获取ac_groups in managers 等于 2 的员工列表。 那么输出应该是:

[
  {
    "_id": 1,
    "name": "user 1",
    "managers": [
      {
        "id": 321,
        "user_id": 1,
        "ack_groups: [
          ["2","3", "4"],
          ["2", "5", "6"]
        ]
      },
      ......
    ] 
  },
  {
    "_id": 2,
    "name": "user 2",
    "managers": [
      {
        "id": 3213,
        "user_id": 2,
        "ack_groups: [
          ["6","7", "8"],
          ["2", "5", "6"]
        ]
      },
      ......
    ] 
  },
]

我在代码中尝试过的内容:Employee::where('verify_routes.managers.ack_groups', '=', "2")->get();

【问题讨论】:

  • 用聚合来做这件事好吗?
  • @mohammadNaimi 你能告诉我如何处理聚合吗?通过进行原始查询?
  • 这是我在 mongodb 中的聚合,而不是 laravel mongoplayground.net/p/t5aI8Drpi6u

标签: php database laravel mongodb nosql


【解决方案1】:

Forum: "Find in array value"

Doc: Specific Operators

这应该可行:

Employee::where('verify_routes.managers.ack_groups', 'all', [2])->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2016-03-18
    • 2020-06-24
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多