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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:06:29+00:00 2026-06-12T03:06:29+00:00

Ok I am making a blog system using MongoDB as the back end. I

  • 0

Ok I am making a blog system using MongoDB as the back end. I want to do the same thing as wordpress when you edit it saves the past versions and allows you to revert to them if needed.

I would like to do the same.

I have a few ways to doing it. but un sure if this is the best easiest way to do it and would like some suggestions.

the first is a find and the insert $SET

<?php
 $cursor = $collection->find(array("_id"=> new MongoId($data)));

        if ($cursor->count() > 0)
        {
              while( $cursor->hasNext() ) {
                foreach($cursor->getNext() as $key => $value)
                {
                    define("_".strtoupper($key), $value);
                }

              }

$cursor = $collection->update(array("_id" => new MongoId($data)),
 '$set'=>array("title"=>$data['TITLE'], "content"=>$data['content'], "past_versons"=>array("title" => _TITLE, "content" => _CONTENT)));
        }
?>

So my question is this the way I would do it.

here a sample JSON

{
"title":"blog title",
"content":"blog content",
"past_verson":[{"title":"blog title past","content":"past blog content"}]
}
  • 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-12T03:06:30+00:00Added an answer on June 12, 2026 at 3:06 am

    In your example, you’re iterating over a MongoCursor even though you only expect to work with one document at most. MongoCollection::findOne() is going to be more appropriate here, as it will return the document array of the first result or null if no documents matched.

    Even if you needed to iterate across multiple results, you’d do better to take advantage of the iterable nature of MongoCursor (it implements Iterator, which extends Traversable, which means you can use it in a foreach loop). I also don’t think define() is appropriate for storing what is essentially a temporary variable here. Moreover, suppose this code needed to run twice (error due to redefinition) or the title or content was not a scalar (another error).

    Consider the following rewrite (I assumed $data['id'] is correct and you made a typo in the above code by using $data alone for the MongoId):

    $document = $collection->findOne(array('_id' => new MongoId($data['id'])));
    
    if (null !== $document) {
        $collection->update(
            array('_id' => new MongoId($data['id'])),
            array(
                '$set' => array(
                    'title' => $data['title'],
                    'content' => $data['content'],
                ),
                '$push' => array(
                    'past_versions' => array(
                        'title' => $document['title'],
                        'content' => $document['content'],
                    ),
                ),
            )
        );
    }
    

    One deviation here, which you may not need, is that I’m aggregating old versions into a past_versions array. We can use the $push operator to append to an array field in the document. Alternatively, you could simply use $set if you only need/want to store the most recent version.

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

Sidebar

Related Questions

I am making a small blog for someone and they want it in 2
My wordpress blog is making use of the newsticker aink scrolling plugin and i'm
Say you're making a blog software, and want to show the number of comments
I am making a personal blog that people can download and edit, but the
I'm making a form on a wordpress blog that uses ajax. I'm trying to
So I have been using blog in blog on my localhost before making it
i'm just started making blog via RoR, using blog-from-scratch guide and i've got some
I am making a blog app, and in that I want to provide the
I am making a blog app and I want to redirect to a different
I am making a blog app in Django and I want to do the

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.