Parse a mongodb document

22 views
Skip to first unread message

Souzanne

unread,
May 28, 2015, 10:21:04 AM5/28/15
to mongod...@googlegroups.com

I am a MongoDB beginner.
I want to parse a mongodb document using PHP.
But I get this error:

Notice: Undefined property: MongoId::$created_at 

This is a part of my document:

{"_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}

And This my PHP code:

$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);

Jeremy Mikola

unread,
May 28, 2015, 11:33:15 AM5/28/15
to mongod...@googlegroups.com
If I understand correctly, your document has three top-level fields:
  • "_id", which contains an ObjectId (i.e. MongoId class in PHP)
  • "0", which contains two fields
    • "created_at", a date string
    • "id": a large floating point number
  • "1", which also contains the same two fields as "0"

In your code example, you're attempting to access a top-level "created_at" field, which doesn't exist.

Generally, "0" and "1" are odd choices for top-level field names. Since you're using PHP, I'm assuming this document resulted from inserting a PHP array, which had sequential, numeric keys. The driver added the "_id" field, but left "0" and "1" in place. Did you expect those embedded documents to be documents in their own right?

Also note that although PHP uses a single array type for lists/vectors and dictionaries/hashes, BSON actually has distinct array and object types (like JSON).

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/c90c1fe0-e89d-49ff-aba4-09afd3522cfb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages