jer*_*rik 2 php arrays push mongodb laravel
在我的mongodb集合中,我想将一些元素推到现有数组中。我使用jenssegers / Laravel-MongoDB-雄辩的模型和查询生成器与lavavel和mongodb一起使用。
如何在jenssegers / Laravel-MongoDB中使用$ push运算符?
{
"_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数组的文档表示形式有些不同。看一下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)
我没有找到任何文档或示例向我展示如何执行此操作。
localhost:27017: Modified field name may not start with $
Run Code Online (Sandbox Code Playgroud)
假设一切正常,因为它可以在外壳中正常工作,然后使用提供的方法进行推送:
Images::where('file.file_id', '=', floatval( $file_id ))
->push('comments', array( 'id' => 4, 'score' => 9 ));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4607 次 |
| 最近记录: |