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

  • Home
  • SEARCH
  • 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 7871953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:03:57+00:00 2026-06-03T02:03:57+00:00

I’m trying to build a simple blog system in PHP with MongoDB just to

  • 0

I’m trying to build a simple blog system in PHP with MongoDB just to give Mongo a try but I’ve ran into some difficulties querying it.

I’ve generated 2000 random posts where each blogpost document looks like this:

{
   "_id": ObjectId("4f908868d3269edc1a00044c"),
   "author": {
     "name": "Christine Skinner",
     "email": "Phyllis_Kirkland@.info",
     "age": 54,
     "city": "London" 
  },
   "post": "sagittis. Nullam vitae diam. Proin dolor. Nulla semper tellus id nunc interdum feugiat. Sed nec metus facilisis lorem tristique aliquet. Phasellus fer [...]",
   "date": "Fri Nov 13 12: 11: 48 +0100 2009",
   "rating": 2,
   "comments": {
     "0": {
       "name": "Barrett Bailey",
       "email": "Velma_Morales@.org",
       "upVotes": 4,
       "downVotes": 76,
       "comment": "gravida. Aliquam tincidunt, nunc ac mattis ornare, lectus ante dictum mi, ac mattis velit justo nec ante. Maecenas mi felis, adipiscing fringilla, por [...]" 
    },
     "1": {
       "name": "Cheryl Fitzgerald",
       "email": "acqueline_Perkins@.org",
       "upVotes": 13,
       "downVotes": 76,
       "comment": "adipiscing. Mauris molestie pharetra nibh. Aliquam ornare, libero at auctor mauris id sapien. Cras dolor dolor, tempus non, lacinia at, iaculis quis, [...]" 
    },
     "2": {
       "name": "Carly Graham",
       "email": "Judah_Santana@.dk",
       "upVotes": 55,
       "downVotes": 75,
       "comment": "Nullam velit dui, semper et, lacinia vitae, massa lobortis ultrices. Vivamus rhoncus. Donec est. Nunc ullamcorper, velit in aliquet lobortis, nisi nib [...]" 
    } 
  },
   "tags": {
     "0": "World War II",
     "1": "world War I" 
  } 
}

I’m pretty much getting a hold on those more advantaged queries when getting for example all posts written by one particular author or getting posts where rating is greater than or equal to X but I’m having some problems with my comments nested array.

I’m not sure if I’ve done it wrong but how do I first of all display all blogs posts with the comments with it? When I’m doing a simple $collection->find() I use the PHP function iterator_to_array to get it all in an array. Then I do a simple foreach() to iterate over each value but of cause the comments are itself an array. Do I have to make a new foreach to get the comments or is there a faster and smarter method to do this?

Another issue I’ve is if I want to use the comments properties in a query. If I’ve to delete a comment, I need to blog _id and the comment author (or the comment number) but the number of comments varies so sometimes there is only one comment and sometimes there are up to 10 comments.

Should the comments be put into a different collection or can I get it to give each comment a more unique ID instead of just the array number? And how do I use the properties inside the comments for querying, let’s say that I want to show one blog post but sort the comments after greatest upVotes?

I’ve tried following several tutorials but most of them are dealing with very static documents so this gives me the thought that I might do this wrong?

Any help or suggestions are very much appreciated.

  • 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-03T02:03:59+00:00Added an answer on June 3, 2026 at 2:03 am

    Firstly, if you want to get a blog post with all the comments in it, you can just do something like:

    $collection->find(array('author.name'=>'Christine Skinner'));
    

    Secondly, yes, if you want to display all the comments, you need another loop, so:

    foreach ($cursor as $post){
       foreach($post['comments'] as $num=>$comment){
          echo "Comment number "
               .$num
               ." was written by "
               .$comment['name']
               .": " 
               .$comment['comment'];
       }
    }
    

    If you want to update a document’s individual comments, you can do that like this:

       $collection->update(
          array('_id'=>new MongoId($id)), 
          array('$set'=>array('comments.'.$num.'.comment'=>$newComment))
    );
    

    You won’t be able to sort the comments sub-document within a query, so you’ll need to sort within PHP after you’ve queried, for each post that you get back. Take a look at usort in the PHP manual, which might do what you need: http://www.php.net/manual/en/function.usort.php.

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

Sidebar

Related Questions

i am trying to build a simple chat system but I'm not sure what
I'm trying to build a simple site but I'm just blocking on this weird
Im trying to build a simple faye realtime based notification system so I can
I'm trying to build an simple publishing tool in jquery. But i'm a bit
I am trying to build a simple vertical thermometer with CSS. I took some
I'm trying to build a simple deployment script for my PHP apps. I know
I'm trying to build simple facebook application with 4.2.1 C# SDK. But I have
I'm trying to build a simple parser, and since InputStream doesn't have some peek-like
I'm trying to build a simple User Template system so that my users can
I am trying to build simple application, just for knowledge. I would like to

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.