douting1871
2017-06-05 07:45
浏览 60
已采纳

laravel-mongodb与嵌套对象的增量

I have a post documents that schema looks like

    {
        "author": {
            "name": "John",
            "gender": 0
        },
        "stats": {
            "likes": 0
        },
        "content": "The new post",
        "updated_at": "2017-06-05 15:15:21",
        "created_at": "2017-06-04 18:10:30",
    }

How can I update the stats.likes field through jenssegers/laravel-mongodb

According to the GitHub page, There are a method increment() that can be used. So I was trying this way

Post::findOrFail( $request->id )->increment('stats.likes');

But got

ErrorException in HasAttributes.php line 906:
Undefined index: stats.likes

However, with the mongo shell, it can work at the code

db.posts.update( { '_id': ObjectId('5933dc7d4abcf141956a6250') }, { '$inc': { 'stats.likes': 1 } )  )

Is that an error caused by nested object?

According to the source code, I found

/**
 * @inheritdoc
 */
public function increment($column, $amount = 1, array $extra = [], array $options = [])
{
    $query = ['$inc' => [$column => $amount]];

    if (! empty($extra)) {
        $query['$set'] = $extra;
    }

    // Protect
    $this->where(function ($query) use ($column) {
        $query->where($column, 'exists', false);

        $query->orWhereNotNull($column);
    });

    return $this->performUpdate($query, $options);
}

If I'm not wrong, the field name stats.likes will pass to the shell directly at the 1st parameter of the method. Why it can just work with the command line?

  • MongoDB 3.4
  • jenssegers/laravel-mongodb 3.2
  • Laravel 5.4

图片转代码服务由CSDN问答提供 功能建议

我有一个帖子文件架构看起来像

  {  
“作者”:{
“名称”:“John”,
“性别”:0 
},
“stats”:{
“赞”:0 
},
“内容 “:”新帖子“,
”updated_at“:”2017-06-05 15:15:21“,
”created_at“:”2017-06-04 18:10:30“,
} \  n   
 
 

如何通过 jenssegers / laravel-mongodb \更新 stats.likes 字段 n

根据GitHub页面,有一个方法 increment()可以使用。 所以我这样做了

  Post :: findOrFail($ request-> id) - > increment('stats.likes'); 
   
 
 

但在HasAttributes.php第906行中得到

  ErrorException:
未定义索引:stats.likes 
  <  / pre> 
 
 

但是,使用mongo shell,它可以在代码

  db.posts.update({'_ id':ObjectId(  '5933dc7d4abcf141956a6250')},{'$ inc':{'stats.likes':1}))
   
 
 

这是嵌套对象引起的错误吗?

根据源代码,我找到了

  / ** 
 * @inheritdoc 
 * / 
 npublic function increment(  $ column,$ amount = 1,array $ extra = [],array $ options = [])
 {
 $ query = ['$ inc'=&gt;  [$ column =&gt;  $ amount]]; 
 
 if(!empty($ extra)){
 $ query ['$ set'] = $ extra; 
} 
 
 // Protect 
 $ this-&gt;  where(function($ query)use($ column){
 $ query-&gt; where($ column,'exists',false); 
 
 $ query-&gt; orWhereNotNull($ column); 
}  ); 
 
返回$ this-&gt; performUpdate($ query,$ options); 
} 
   
 
 

如果我没错,那么该字段 name stats.likes 将直接在方法的1st参数传递给shell。 为什么它可以使用命令行?

  • MongoDB 3.4
  • jenssegers / laravel-mongodb 3.2 < li> Laravel 5.4
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsajdgjadwqe3247382 2017-06-07 09:02
    已采纳

    Since I can update a nested object through the shell, I wrote a raw expression below alternatively:

    $result = Post::raw(function($collection) use ($request){
    
        return $collection->findOneAndUpdate([
            '_id' => new \MongoDB\BSON\ObjectID ($request->_id),
        ], [ '$inc' => [ 'stats.likes' => 1 ] ] );
    
    });
    

    Notice that you may have effect with db.articles.update(), but the Mongo\Collection doesn't have a method named update(), use findOneAndUpdate, or others instead. (UpdateMany, UpdateOne)

    And if the where condition contains _id, you have to wrap your id variable with \MongoDB\BSON\ObjectID object in the case of raw expression.

    点赞 打赏 评论

相关推荐 更多相似问题