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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:32:00+00:00 2026-05-27T20:32:00+00:00

document structure example is: { dob: 12-13-2001, name: Kam, visits: { 0: { service_date:

  • 0

document structure example is:

{
   "dob": "12-13-2001",
   "name": "Kam",

   "visits": {
     "0": {
       "service_date": "12-5-2011",
       "payment": "40",
       "chk_number": "1234455",  
    },
     "1": {
       "service_date": "12-15-2011",
       "payment": "45",
       "chk_number": "3461234",  
    },
     "2": {
       "service_date": "12-25-2011",
       "payment": "25",
       "chk_number": "9821234",  
    } 
  } 
}

i am trying to update only “payment” in visit – 2 and chk_number in visit – 1 using following:


$query_criteria =  array("name" => "Kam", "dob" => "12-13-2001");

$updated_data = array();

$updated_data['visits'][1]['chk_number'] = 567;
$updated_data['visits'][2]['payment'] = 100;

$ret = $collection->update( $query_criteria, array('$set' => $updated_data), array("safe" => true) );

But this update deletes all the visits and overwrite them with vists containing only payment and chk_number .

I am new to mongodb and PHP. Can anyone please point me towards what wrong am i doing and how to fix this problem.

Many thanks in advance……..

  • 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-05-27T20:32:01+00:00Added an answer on May 27, 2026 at 8:32 pm

    You should use a dot notation.

    db.collection.update(query_criteria, 
                         {$set: {'visit.2.payment': 100, 
                                 'visit.1.chk_number': 567}});
    

    What you’re doing is this:

    db.collection.update(query_criteria, 
                         {'visit' : {'2' : {'payment': 100}}, 
                                    {'1': {'chk_number': 567}}});
    

    And it basically means “find a document and overwrite it with this new one”.

    The code I wrote are in Javascript (as they would appear in mongo shell). I leave it up to you to translate to proper PHP (I’m not a PHP guy).

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

Sidebar

Related Questions

I'd like to write a document, for example: deploy procedure software structure DB structure
I'm trying to design an XML document structure for my application. I want to
I have a given XML document (structure can not be changed) and want to
I'm writing a Java TextComponent where the underlying document has some structure. It is
(Note: I realize this is close to How do you document your database structure?
I need to reinforce the structure of a Microsoft Word document. EG: Style Question
I'm working with an existing XML document which has a structure (in part) like
I'm working with an XML document that contains a structure that looks similar to
If I have a table structure like this: Transaction [TransID, ...] Document [DocID, TransID,
I call an XML document three-layered if its structure is laid out as following:

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.