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 8550701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:00:51+00:00 2026-06-11T14:00:51+00:00

I am trying to add a comment to entries into mongodb. This is what

  • 0

I am trying to add a comment to entries into mongodb.
This is what i have so far

$mongo = new Mongo();
$db = $mongo->comedy;
$collection = $db->cartoons;
$obj = array( 
    "title" => "football", array('comment' => 'my comment here'),
    "author" => "joe" 
);

$collection->insert($obj);

which produces this entry

{
 "_id": ObjectId("5059fd31ba76883414000001"),
 "title": "football",
 "0": {
     "comment": "my comment here"
},
 "author": "joe"
}   

my question is this the best way to nest a comment under the entry “football”? or should i be going about it a different way?

this part doesnt seem correct

"0": {
     "comment": "my comment here"
}

update
from the example below, running this gave an error Fatal error: Call to undefined method MongoDB::update()

$mongo = new Mongo();
$db = $mongo->comedy;
$collection = $db->cartoons;

$mongo->comedy->update(array('title' => 'football'), array(
        '$push' => array('comments' => array('content' => 'Yo!', 'author' => $user_id))
));

then when i run it like

$mongo = new Mongo();
$db = $mongo->comedy;
$collection = $db->cartoons;
$obj = array(
    '$set' => array("title" => "football", "author" => "joe"), 
    '$push' => array('comments' => array('content' => 'Yo!'))
);

i get

{
 "_id": ObjectId("505a2493ba76883c08000007"),
 "title": "football",
 "0": {
     "$push": {
         "comments": {
             "content": "Yo!"
        }
    }
},
 "author": "joe"
}   
  • 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-11T14:00:52+00:00Added an answer on June 11, 2026 at 2:00 pm

    This is a very typical problem in MongoDB and as a noob it too got me once here is a good structure (you can Google this around):

    {
        title: footbal,
        comments: [
            {content: '', author: ObjectId()},
            { // Next comment }
        ]
    }
    

    You can then $push to the comments field making all comments be sorted from newest to
    oldest.

    As a word of caution: You might find this schema a bit restrictive on it’s querying possibilities especially when you want to, in realtime, sort the comments a different way or pick out different types of comments. In this scenario you would use a separate collection to house the comments ideally.

    Edit

    In PHP you would start off by inserting your document:

    $mongo = new Mongo();
    $db = $mongo->comedy;
    $collection = $db->cartoons;
    $obj = array( "title" => "football");
    $collection->insert($obj);
    

    And then when a new comment needs to be added just simply $push:

    $mongo->comedy->update(array('title' => 'football'), array(
        '$push' => array('comments' => array('content' => 'Yo!', 'author' => $user_id))
    ));
    

    And that’s the simple method of doing it 🙂

    Edit again

    $obj = array('$set' => array("title" => "football", "author" => "joe"), '$push' => array('comments' => array('content' => 'Yo!'))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add comments to a Post model class Comment < ActiveRecord::Base belongs_to
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a class object into a List using reflection, but when invoking
I'm trying to add comment in php line. But I don't know if a
I have a working Ajax form, now I'm trying to add file upload to
I'm trying to allow comments by anon users but the add comment form, add
I have been trying to implement a comment engine in my app (UItableView) but
I'm trying to create an add comment form on a page, but it'll need
For my application, I am trying to add entries without having a duplicate entry,
i am trying to do a simple form that adds a new comment using

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.