Laravel / Mongodb -如何构建查询,其中对象数组计数等于竞争对手总数

i1icjdpr  于 2023-02-15  发布在  Go
关注(0)|答案(1)|浏览(93)

我在mongodb服务器上有一个对象数组**(matching_competents)**:

"matching_competitors": [
    {
      "id_competitor": "21",
      "domain": "",
      "id_product_competitor": 224731,
      "url_product_competior": "https://www.librarie.net/p/224731/nia=1693"
    },
    {
      "id_competitor": "21",
      "domain": "",
      "id_product_competitor": 282868,
      "url_product_competior": "https://www.librarie.net/p/282868/nia=1693"
    }
  ]

我在竞争对手表(mysql)中有3个竞争对手
现在,我需要构建一个查询,其中-〉matchig_competents的大小(mongodb)应该等于总竞争对手(mysql)
我不知道如何建立这个查询。
我的当前查询:

$get_product = DB::connection('mongodb')->collection( 'products_' . $get_project_id->id_project )->Where( 'matching_competitors', $total_competitor )->paginate(6);
juzqafwq

juzqafwq1#

你可以这样做。

$competitorCount = DB::table('competitors')->count();

$get_product = DB::connection('mongodb')
    ->collection('products_' . $get_project_id->id_project)
    ->whereRaw(['$expr' => ['$eq' => ['$size' => '$matching_competitors'], $competitorCount]])
    ->paginate(6);

相关问题