我是MongoDB的初学者。
我想解析一个mongodb文件。
但是我收到了这个错误:
Notice: Undefined property: MongoId::$created_at
{"_id":{"$id":"554351e5fd2897ac42000029"},
"0":{"created_at":"Thu Apr 30 12:25:12 +0000 2015",
"id":5.9375296462128e+17},
"1":{"created_at":"Thu Apr 30 12:25:12 +0000 2015",
"id":5.93277777128e+17}
这是我的PHP代码:
$cursor = (object)$collection->findOne( $query );
$l = 0;
foreach ($cursor as $twt) {
$twt = (object) $twt;
$test[$l] = (object)(
$twt->created_at
);
$l = $l +1; }
echo json_encode($test);`
答案 0 :(得分:0)
您的对象数据结构有点奇怪,看起来您有一个“0”和一个“1”键。您的createdat属性位于“0”键内。尝试展平您的对象,使createdat
属性位于顶层,与_id相同
答案 1 :(得分:0)
这对我有用。
$cursor =$collection->findOne( $query );
unset($cursor["_id"]);
$cursor= (object)$cursor;
$l = 0;
foreach ($cursor as $twt) {
$twts = (object) $twt;
$test[$l] = (object)(
$twts->created_at
);
$l = $l +1; }