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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:59:05+00:00 2026-05-31T04:59:05+00:00

I’m experimenting with MongoDB using the PHP PECL extension, however I’m having difficulty getting

  • 0

I’m experimenting with MongoDB using the PHP PECL extension, however I’m having difficulty getting a certain update query to work. I have searched around on SO for answers with little luck.

I have created a basic collection:

$m = new Mongo;
$collection = $m->testdb->testcollection;

$collection->insert(array(
    0, 1, 1, 2, 3, 5
));

Using findOne and var_dump the record appears as follows:

array
  '_id' => 
    object(MongoId)[6]
      public '$id' => string '4f3bde65a1f7a0315b000000' (length=24)
  0 => int 0
  1 => int 1
  2 => int 1
  3 => int 2
  4 => int 3
  5 => int 5

The problem comes when I want to update using $set. I am basing my query on the mapping shown towards the bottom of the SQL to Mongo Cheat Sheet in the PHP manual

Here I want to update field 0 to value 100

$obj = $collection->findOne();

$collection->update(
    array('_id' => $obj['_id']),
    array('$set' => array(0 => 100))
);

Re-fetching the record shows that it remains unchanged.

I did wonder if I was doing something wrong with the _id, however the following update query does work, albeit replacing the entire record with a new value, not simply updating the one field.

$collection->update(
    array('_id' => $obj['_id']),
    array(0 => 100)
);

Object dump:

array
  '_id' => 
    object(MongoId)[7]
      public '$id' => string '4f3bde65a1f7a0315b000000' (length=24)
  0 => int 100

Can someone please point out what I am doing wrong, and how to properly use $set. I’m sure it’s obvious and I just need a second pair of eyes on it.

Many thanks.

  • 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-31T04:59:06+00:00Added an answer on May 31, 2026 at 4:59 am

    I’ve doing some investigations to why this happens. And I don’t think I can find a way on how to “fix” this issue.

    JavaScript has a difference between arrays and associative arrays/objects. PHP has the difference between arrays and objects. For PHP an associative array is an array, and for JavaScript it is an object.

    When the PHP driver needs to convert an array to a JSON object, it tries to figure out whether an array is either: a normal array with sequentially numbered keys starting with 0; or an associative array. The current implementation regards any array with sequentially numbered keys, starting from 0 an normal array. And a normal array does not contain keys. And this is the problem. In the situation the driver sees a normal array, there is no field name information in the BSON that’s send to the server, and hence the server can’t update a field.

    I can’t think of a way to change this behaviour without breaking any sort of existing code. So if you want numerical fieldnames, you will have to use a stdClass object for the “main document”. Alternatively, you could push those keys into a embedded document and then update:

    <?php
    $m = new Mongo;
    $collection = $m->demo->testcollection;
    
    $collection->insert(array(
        "_id" => 'bug341',
        'data' => array( 0, 1, 1, 2, 3, 5 )
    ));
    
    $obj = $collection->findOne();
    
    $update = array('data.0' => 'zero int');
    
    $collection->update(
        array( '_id' => 'bug341' ),
        array( '$set' => $update )
    );
    
    
    $obj = $collection->findOne();
    var_dump($obj);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words

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.