Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:04:52+00:00 2026-06-01T12:04:52+00:00

I’ve accessed the Facebook Graph API to get a JSON object representing the latest

  • 0

I’ve accessed the Facebook Graph API to get a JSON object representing the latest posts on my feed (my Facebook wall). I then saved it into a MongoDB collection called feeds using the PHP Mongo Driver.

//$post['feed']['data'] contains the Facebook JSON object of wall posts
//create a mongo instance
$mongo = new Mongo();
//access the feeds collection
$feeds = $mongo->changeup->feeds;
//dump the feed right into mongo
$feeds->insert($post['feed']['data']);

This is what one of the arrays looks like after reading back the whole object that was placed into mongo.

I’m only showing you one, but it gives me several more, each indexed, the next one is [1] => Array() and so on… some are structured differently, as some contain the [story] field, others contain the [message] field, and some contain both.

Query:
$cursor = $feeds->find();

foreach ( $cursor as $feed ) { 
print_r($feed);
}

Result:
[0] => Array
        (
            [id] => 505212695_10150696450097696
            [from] => Array
                (
                    [name] => John Doe
                    [id] => 505212695
                )

            [story] => "Text of a story I posted on my wall..."
            [story_tags] => Array
                (
                    [38] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 15212444
                                    [name] => John Doe
                                    [offset] => 38
                                    [length] => 10
                                    [type] => user
                                )

                        )

                )

            [type] => status
            [application] => Array
                (
                    [name] => Share_bookmarklet
                    [id] => 5085647995
                )

            [created_time] => 2012-04-04T05:51:21+0000
            [updated_time] => 2012-04-04T05:51:21+0000
            [comments] => Array
                (
                    [count] => 0
                )

)

The problem is that I don’t want to just find the entire collection, I want to find only those arrays that have say [message] and [story] fields, and then just find their contents and nothing else.

I’m trying to receive a subset, two levels deep:

//this works, however, I'm only able to get the 0 array 
$cursor = $feeds->find( array(), array('0.story' => true) );

How do I filter by all arrays?

I want my end result to look like this:

Array
(
    [_id] => MongoId Object
        (
            [$id] => 4f7db4dd6434e64959000000
        )

    [0] => Array
        (
            [story] => "Text of a story I posted on my wall..."
        )
    [1] => Array
        (
            [story] => "Text of a story I posted on my wall..."
        )
    [2] => Array 
        (
            [story] => "Text of a story I posted on my wall..."
            [message] => "In this case message text exists as well..."
        )
    [3] => Array
        (
            [message] => "Text of a message I posted on my wall..."
        )

    etc...
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:04:53+00:00Added an answer on June 1, 2026 at 12:04 pm

    I believe the initial issue starts with your data structure for each feed document. Notice that your object is simply an id, and then an incrementing amount of number keys, and thats it. What would be ideal is that you insert an actual object structure, with keys and values, at the top level. Currently, because you directly dumped the facebook data straight into mongo without formatting it, the driver mapped your array to key/value. Now each feed doc has variable amount of anonymous objects.

    Refer to this: http://www.php.net/manual/en/mongo.writes.php

    What I would think your feed doc should look like might be this:

    { 
        "_id" : ObjectId("4f7db4dd6434e64959000000"), 
        "posts" : 
        [
            {
                "story" : "Text of a story I posted on my wall...",
                "message" : "In this case message text exists as well...",
            },
            {
                "story" : "Text of a story I posted on my wall...",
                "message" : "In this case message text exists as well...",
            }
        ],
        "posts_meta1": "some val",
        "posts_meta2": "other data"
    }
    

    Notice that it contains a “posts” top level key, with your array of post objects underneath. This fixes multiple issues. You have a top level key to index with, instead of “number”, you have a cleaner root level for adding more feed fields, and you can cleanly achieve your find query.

    A simple find might look like this:

    // Return all feed docs, and only include the posts.story field
    db.feeds.find({}, {"posts.story": 1})
    

    A more advanced query might look like this:

    // Return an feed document that either contains a posts.story
    // field, or, contains a posts.message field
    db.feeds.find({
        $or: [ 
            {$exists: {"posts.story": true}}, 
            {$exists: {"posts.message": true} 
        ]
    })
    

    In a nutshell, your data returned from facebook should be formatted first into an object structure, and then inserted into mongo. For instance, dates should be inserted as proper date objects as opposed to raw strings: http://www.php.net/manual/en/class.mongodate.php. This allows you to then do date-based queries in mongo, and the php driver will also make sure to convert them back and forth so that they are more native to your language.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
i got an object with contents of html markup in it, for example: string
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.