【问题标题】:Using where in jenssegers/laravel-mongodb在 jenssegers/laravel-mongodb 中使用 where
【发布时间】:2020-07-14 05:52:59
【问题描述】:

我在 laravel 中使用jenssegers/laravel-mongodb

我的数据在mongodb中的结构如下。假设有 100 行这样的

我需要一个查询来显示finance.selling_type 状态为 1 的所有数据

我尝试编写代码

finances::where('finance.selling_types.$$status', 1)->get();

finances::where('finance.selling_types.*.status', 1)->get();

【问题讨论】:

  • where条件DB::collection('finances')->where('finance.sellingTypes.status',1)->get()内不用*$$可以找到文件
  • 这个答案是错误的。请注意,对象“sales_types”的每个头部本身就是一个对象

标签: laravel mongodb where-clause


【解决方案1】:

使用aggregation pipeline

将 sell_types 中嵌入的对象转换为数组

 $convertSellingTypesToArray = [
    '$project' => [
      'finance.selling_types' => [
        '$objectToArray' => '$finance.selling_types'
      ]
    ]
  ];

然后将数组展开为单个文档

$sellingTypesAsDocuments =   [
    '$unwind' => '$finance.selling_types'
];

过滤状态为 1 的位置。

$selectOnlyActiveStatus = [
    '$match' => [
      'finance.selling_types.v.status' => [
        '$eq' => 1
      ]
    ]
  ];

将 sell_types 转换回一个对象。

$convertSellingTypesToObject = [
    '$project' => [
      'finance.selling_types' => [
        '$arrayToObject' => [
          [
            '$finance.selling_types'
          ],
        ]
      ]
    ]
  ];
finances::raw()->aggregate([
  $convertSellingTypesToArray,
  $sellingTypesAsDocuments,
  $selectOnlyActiveStatus,
  $convertSellingTypesToObject,
])

请注意,您需要在查询中支持分页

【讨论】:

    【解决方案2】:

    感谢 Oluwafemi Sule,我找到了解决方案:

    \DB::connection('mongodb')
                ->collection('finances')
                ->raw(function($collection) {
                    return $collection->aggregate(
                        [
                            [
                                '$project' => [
                                    'finance'=> [
                                        'selling_types'=> [
                                            '$objectToArray'=>  '$finance.selling_types'
                                        ]
                                    ],
                                  
                                ]
                            ],
                            [
                                '$unwind' => '$finance.selling_types'
                            ],
                            [
                                '$match'=> [
                                    '$and' => [
                                        [
                                            'finance.selling_types.v.status' => [
                                                '$eq' => 1
                                            ],
                                        ],
                                    ]
                                ]
                            ],
    
                        ]
                    );
                });
    

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 1970-01-01
      • 2017-06-15
      • 2014-08-04
      • 2018-12-28
      • 2017-05-21
      • 2015-02-13
      • 2016-07-26
      • 2018-12-25
      相关资源
      最近更新 更多