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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:44:29+00:00 2026-06-11T12:44:29+00:00

I ve the following content in mongo db collection, { _id : ObjectId(5052f343381ef8bc10000011), first_name

  • 0

I ve the following content in mongo db collection,

{ "_id" : ObjectId("5052f343381ef8bc10000011"), "first_name" : "Tadataka", "midle_name"  :      
"", "last_name" : "Yamada", "title" : "Independent Director", "biogra
phy" : "Dr. Tadataka Yamada, M.D., is Independent Director of Agilent Technologi
es Inc., ", "rank" : " ", "department" : " ", "current" : "true", "company_id" :     ObjectId("50072714b4a6deba100051d3"
) } }

When I tried to update the above content for few fields and also insert new field if not exist, the existing fields get over written. Here is my code,

$mycollection->update(array("_id" => $id), array('$set' => array("first_name" => $first_name, "updated_at" => $uat));

and the result I am getting is

{ "_id" : ObjectId("5052f343381ef8bc10000011"), "first_name" : "Tadataka", "updated_at" : 134567894 }

Instead of updating just the values, my whole content gets overwritten, where I am missing.?

  • 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-11T12:44:30+00:00Added an answer on June 11, 2026 at 12:44 pm

    The update() in the code you shared is correct. Since you’re using an atomic modifier, $set, there is no reason that the entire document for the given _id would be overwritten. The following script easily reproduces this, and outputs the example document with its first_name field altered and updated_at field added:

    <?php
    
    $m = new Mongo();
    $c = $m->test->foo;
    $c->drop();
    
    $document = array(
        '_id' => new MongoId('5052f343381ef8bc10000011'),
        'first_name' => 'Tadataka',
        'middle_name' => '',
        'last_name' => 'Yamada',
        'title' => 'Independent Director',
        'company_id' => new MongoId('50072714b4a6deba100051d3')
    );
    
    $c->insert($document);
    
    $c->update(
        array('_id' => $document['_id']),
        array('$set' => array('first_name' => 'foobar', 'updated_at' => new MongoDate()))
    );
    
    var_dump($c->findOne());
    

    If you run this locally, you should see:

    array(7) {
      ["_id"]=>
      object(MongoId)#8 (1) {
        ["$id"]=>
        string(24) "5052f343381ef8bc10000011"
      }
      ["company_id"]=>
      object(MongoId)#9 (1) {
        ["$id"]=>
        string(24) "50072714b4a6deba100051d3"
      }
      ["first_name"]=>
      string(6) "foobar"
      ["last_name"]=>
      string(6) "Yamada"
      ["midle_name"]=>
      string(0) ""
      ["title"]=>
      string(20) "Independent Director"
      ["update_at"]=>
      object(MongoDate)#10 (2) {
        ["sec"]=>
        int(1347908855)
        ["usec"]=>
        int(551000)
      }
    }
    

    It would probably be helpful if you shared how you obtained the last result you pasted, but your update query certainly isn’t to blame here.

    As a side note, you likely want to use a proper boolean value for the current field in your document instead of the string "true".

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

Sidebar

Related Questions

Imagine the following content types: Content Type A - Title - Field with a
I have a JSON file my.json with the following content: { title: My Title,
Let's say I have the following content: Lorem Ipsum is simply dummy text of
I have a file containing the following content 1000 line in the following format:
I have an .htaccess file with following content: AddHandler x-httpd-php5 .php For some reason,
I have an html page with the following content: [...] <tr><td class=n><a href=play-1.0.1.zip>play-1.0.1.zip</a></td></tr> <tr><td
I have a xPage with following content: <xe:restService id=restService preventDojoStore=false> <xe:this.service> <xe:viewJsonService viewName=vwChartData contentType=text/json>
I have a txt file named 'a.txt' with the following content: Hi=Python Now, I
I have a file test.xml with the following content : <body> <content> <p>Lorem ipsum
I have a file ( myenv.sh ) with the following content export MYVAR=HELLO And

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.