laravel mongodb将元素推到document_中的现有数组

jer*_*rik 2 php arrays push mongodb laravel

在我的mongodb集合中,我想将一些元素推到现有数组中。我使用jenssegers / Laravel-MongoDB-雄辩的模型和查询生成器与lavavel和mongodb一起使用。

如何在jenssegers / Laravel-MongoDB中使用$ push运算符?

应更新的MongoDB条目(RockMongo表示形式):

{
   "_id": ObjectId("5328bc2627784fdb1a6cd398"),
   "comments": {
     "0": {
       "id": 3,
       "score": 8
    }
  },
   "created_at": ISODate("2014-03-18T21:35:34.0Z"),
   "file": {
     "file_id": NumberLong(1175),
     "timestamp": NumberLong(1395178534)
  }
}
Run Code Online (Sandbox Code Playgroud)

关于Rockmongo和Mongo Shell中数组表示的提示

RockMongomongo shell数组的文档表示形式有些不同。看一下comments-array。上面的RockMongo表示形式在mongo shell中显示为:

{ 
    "_id" : ObjectId("5328c33a27784f3b096cd39b"), 
    "comments" : [  
     {  
        "id" : 3,  
        "score" : 8 
     } 
    ], 
    "created_at" : ISODate("2014-03-18T22:05:46Z"), 
    "file" : { 
        "file_id" : NumberLong(1176), 
        "timestamp" : NumberLong(1395180346) 
    } 
}
Run Code Online (Sandbox Code Playgroud)

如文档所述,$push操作员将元素推入数组。这在mongo-shell中可以正常工作:

蒙哥壳

db.Images.update({'file.file_id': 1175}, 
   { $push: { comments: { id: 3, score: 8} } 
})
Run Code Online (Sandbox Code Playgroud)

但是在查询构建器中,我很难合并$ push运算符。我得到错误:

localhost:27017: Modified field name may not start with $
Run Code Online (Sandbox Code Playgroud)

我没有找到任何文档或示例向我展示如何执行此操作。

我的jenssegers / Laravel-MongoDB代码返回错误

localhost:27017: Modified field name may not start with $
Run Code Online (Sandbox Code Playgroud)

Nei*_*unn 6

假设一切正常,因为它可以在外壳中正常工作,然后使用提供的方法进行推送

Images::where('file.file_id', '=', floatval( $file_id ))
   ->push('comments', array( 'id' => 4, 'score' => 9 ));
Run Code Online (Sandbox Code Playgroud)