douweida2878
2016-11-17 05:16
浏览 170
已采纳

通过运行laravel raw mongodb查询选择最新记录

I'm trying ti get the latest inserted record of mongodb as a collection in laravel using moloquent (jessenger/mongodb) package.

But I can not figure out how to run this raw query in laravel :

db.collection.find().limit(1).sort({$natural:-1})

I know the formate of raw query in laravel is :

$bcamps = Bcamp::raw()->find(my raw query);

But how can I return the last inserted record as a collection by running that raw query in laravel 5.3 ?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyong4623 2016-11-17 06:17
    已采纳

    I've figured how to do that by selecting the last record and then get it as a collection to be able to update it.

    $bcamps = Bcamp::raw()->findOne([],['sort' => ['_id' => -1],'projection' => ['_id' => 1]]);
    
    $bcamps = Bcamp::where('_id', '=' , $bcamps->_id)->first();
    

    Hope to save some other people time.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题