Mongo + Laravel:访问子文档的子文档

时间:2017-09-15 15:15:51

标签: javascript php mongodb laravel

我正在使用jenssegers mongodb包将Laravel与mongo连接,我正在尝试访问Laravel中子文档的子文档,以便删除具有特定值的所有数据: 例如,在这里,我想删除所有elec等于4的子子目录。

family {
    "_id" = 12,
    subfamily{
         "_id" = 123,
         "name" = abcde,
         subsubfamily{
             "_id" = 1234,
             elec = 5
         },
         {
             "_id" = 5678,
             elec = 4
         }
    },
    {
         "_id" = 456,
         "name" = lmnop,
         subsubfamily{
             "_id" = 9012,
             elec = 4
         },
         {
             "_id" = 3456,
             elec = 8
         }
    }  
}

我试图通过以下方式访问该子系列:

$res= family::where('_id',12)->where('subfamily._id', 456)->project(array('subfamily.$' => 1))->get();

(返回相同的结果:

 db.collection.find({"subfamily" : {"$elemMatch" : {"subsubfamily" : {"$elemMatch" : {"_id" : 9012}}}}},{"measurements.$":1})

它返回我想要的子系列:

"_id" => 12
"subfamily" => array:1 [
  0 => array:3 [
        "_id" => 456
        "name" => lmnop
        "subsubfamily" => array:2 [
            0 => array:2 [
               "id" => 9012
               "elec" => 4
            ]
            1 => array:2 [
               "id" => 3456
               "elec" => 8
            ]
  ]

但我无法弄清楚如何访问子系列。我想要这样的事情:

"subsubfamily" => array:1 [
            0 => array:2 [
               "id" => 9012
               "elec" => 4
            ]

然后我就可以删除它了。

$res= family::where('_id',12)->where('subsubfamily._id', 9012)->project(array('subfamily.$' => 1))->get();

不工作。

$res= family::where('_id',12)->where('subfamily._id', 456)->where('subsubfamily._id', 9012)->project(array('subfamily.subsubfamily' => 1))->get();

也不工作。

任何人都可以提供帮助吗?

0 个答案:

没有答案