使用_id时,MongoDB \ Client无法检索

时间:2018-10-04 19:52:28

标签: mongodb laravel-5.6

我正在尝试从laravel中的mongodb数据库中引用一些数据

此内容取自作曲家文件以供参考:"mongodb/mongodb": "^1.3",

以下工作正常:

$collection->findOne( array( 'user_id' => (int)Auth::user()->id ) ); 

但是我需要在_id列中引用数据

当我执行以下操作时:

$id = "5bb390bf421aa9bf4a1a85b7";
$details = $collection->findOne( array('_id' => $id) );
dd($details);

它只是返回null

这是mongodb表:

{ 
    "_id" : ObjectId("5bb390bf421aa9bf4a1a85b7"), 
    "auction_id" : NumberInt(36), 
    "user_id" : NumberInt(3), 
    "currency" : "0", 
    "status" : "3"
}
{ 
    "_id" : ObjectId("5bb390bf421aa9bf4b7540a7"), 
    "auction_id" : NumberInt(35), 
    "user_id" : NumberInt(3), 
    "currency" : "1", 
    "status" : "2"
}
{ 
    "_id" : ObjectId("5bb390bf421aa9bf4a1a85b8"), 
    "auction_id" : NumberInt(36), 
    "user_id" : NumberInt(3),
    "currency" : "0", 
    "status" : "1"
}

添加:

如果我尝试:

$id = "5bb390bf421aa9bf4a1a85b7";
$details = $collection->findOne( array('_id' => ObjectId($id) ) );
dd($details);

我收到此错误:

Call to undefined function App\Http\Controllers\ObjectId()

也尝试过:

$details = $collection->findOne( array('_id' => new MongoDB\BSON\ObjectId("5bb390bf421aa9bf4a1a85b8") ) );

,出现以下错误:

Class 'App\Http\Controllers\MongoDB\BSON\ObjectId' not found

0 个答案:

没有答案