更新mongodb中对象数组内的特定对象

时间:2018-03-14 13:36:50

标签: mongodb laravel

我有以下文件:

{
   _id: objectId("5aa7abb3c7dc133b0007e6e"),
   name: "first course",
   targets: Array
     0: Object
        id: "11111_rrrr",
        value: "test"
     1: Object
        id: "22222_rrrr",
        value: "hi"
}

所以,我想改变数组[i]的值。 我的解决方案是:

$update = array(
        'targets.$[].value' => "New Value"
    );

    $result = Course::where("_id", $course_id)->update($update);

它改变了数组的所有值,我怎样才能解决我的问题?

1 个答案:

答案 0 :(得分:0)

试试这个

df %>%
   filter(row_number() <= sum(vec)) %>% #based on the comments from OP
   mutate(Subset = rep(vec, vec)) %>%
   group_by(grp = rep(seq_along(vec), vec), Blocks) %>%
   summarise(Subset = first(Subset), n = n())%>%
   ungroup %>%
   spread(Blocks, n, fill = 0) %>%
   select(-grp)
# A tibble: 3 x 5
#   Subset  `2D`  `2E`  `4F`  `5E`
#*  <dbl> <dbl> <dbl> <dbl> <dbl>
#1   2.00  1.00  1.00  0     0   
#2   3.00  1.00  0     1.00  1.00
#3   2.00  2.00  0     0     0