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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22491889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:13:41  来源:igfitidea点击:

laravel mongodb push element to existing array in document_

phparraysmongodblaravelpush

提问by jerik

In my mongodb collection I want to push some elements to an existing array. I use jenssegers/Laravel-MongoDB - Eloquent model and Query builderto work with lavavel and mongodb.

在我的 mongodb 集合中,我想将一些元素推送到现有数组。我使用jenssegers/Laravel-MongoDB - Eloquent 模型和查询构建器来处理 lavavel 和 mongodb。

How can I use the $push operator in jenssegers/Laravel-MongoDB?

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

MongoDB entry which shall be updated (RockMongo representation):

应更新的 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)
  }
}


Hint about array representation in rockmongo and mongo shell

关于rockmongo 和mongo shell 中的数组表示的提示

RockMongoand mongo shellarray representation of the documents are a little bit different. Have a look at the comments-array. The above RockMongo representation appears in the mongo shell as:

文档的 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) 
    } 
}


As the documentation states the $pushoperater to push elements to an array. This works fine in the mongo-shell:

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

Mongo shell

蒙戈壳

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

But in the query-builder I struggle to incorporate the $push operator. I get the error:

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

localhost:27017: Modified field name may not start with $

I did not find any documentation or example that showed me how to do it..

我没有找到任何文档或示例向我展示了如何做到这一点..

My jenssegers/Laravel-MongoDB code, that returns the error

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

// $file_id = 1175
public static function addComment( $file_id ) {  
    $image = Images::where( 'file.file_id', '=', floatval( $file_id ) )
    ->update( array('$push' => array( 'comments' => array( 'id' => 4, 'score' => 9 ) ) ) );
    return $image; 
} 

回答by Neil Lunn

Assuming everything is okay as it works in the shell then use the provided method to pushinstead:

假设一切正常,因为它在 shell 中工作,然后使用提供的方法来推送

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