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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:48:27+00:00 2026-06-02T13:48:27+00:00

How to update value of the nth position element inside an array? I want

  • 0

How to update value of the nth position element inside an array?

I want to update index 2 of category_id.

One solution is ($pop or $pull) and $addToSet.

Is there any other solution?

Array ( [_id] => MongoId Object ( [$id] => 4f93ae990c211da128000004 ) 
        [name] => UnderWater Photography Tips 
        [category_id] => Array ( 
                    [0] => MongoId Object ( [$id] => 4f93a5050c211da328000001 ) 
                    [1] => MongoId Object ( [$id] => 4f93a8860c211da128000003 ) 
                    [2] => ) 
        [user_id] => MongoId Object ( [$id] => 4f92565a0c211dd706000001 ) ) 
  • 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-02T13:48:28+00:00Added an answer on June 2, 2026 at 1:48 pm

    You can do this by using the array element by position functionality. In PHP, that would look like doing:

    $c->update(
        array( '_id' => new MongoID( '4f93ae990c211da128000004' ) ),
        array( '$set' => array(
            'category_id.2' => new MongoID( '4f93a5050c211da328009999' )
        ))
    );
    

    On the shell, you would run:

    db.arraytest.update(
        { _id: ObjectId( '4f93ae990c211da128000004' ) },
        { $set: { 'category_id.2' : ObjectId( '4f93a5050c211da328009900' ) } }
    );
    

    When I execute the following script, it shows exactly what happens:

    <?php
    $m = new Mongo();
    $c = $m->demo->arraytest;
    
    $c->drop();
    $c->insert( array(
        '_id' => new MongoID( '4f93ae990c211da128000004' ),
        'name' => 'Underwater',
        'category_id' => array(
            new MongoID( '4f93a5050c211da328000001' ),
            new MongoID( '4f93a5050c211da328000002' ),
            new MongoID( '4f93a5050c211da328000003' ),
            new MongoID( '4f93a5050c211da328000004' ),
        ),
        'user_id' => new MongoID( '4f92565a0c211dd706000001' )
    ) );
    
    $c->update(
        array( '_id' => new MongoID( '4f93ae990c211da128000004' ) ),
        array( '$set' => array(
            'category_id.2' => new MongoID( '4f93a5050c211da328009999' )
        ))
    );
    
    var_dump( $c->findOne( array( '_id' => new MongoID( '4f93ae990c211da128000004' ) ) ) );
    

    Now run it:

    $ php test.php
    array(4) {
      ["_id"]=>
      object(MongoId)#6 (1) {
        ["$id"]=>
        string(24) "4f93ae990c211da128000004"
      }
      ["name"]=>
      string(10) "Underwater"
      ["category_id"]=>
      array(4) {
        [0]=>
        object(MongoId)#5 (1) {
          ["$id"]=>
          string(24) "4f93a5050c211da328000001"
        }
        [1]=>
        object(MongoId)#4 (1) {
          ["$id"]=>
          string(24) "4f93a5050c211da328000002"
        }
        [2]=>
        object(MongoId)#11 (1) {
          ["$id"]=>
          string(24) "4f93a5050c211da328009999"
        }
        [3]=>
        object(MongoId)#10 (1) {
          ["$id"]=>
          string(24) "4f93a5050c211da328000004"
        }
      }
      ["user_id"]=>
      object(MongoId)#12 (1) {
        ["$id"]=>
        string(24) "4f92565a0c211dd706000001"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update one value of a compound primary key from within the
I want to always update the value of an update row in the database.
I have one table TABLE_EXAM . i need to update the value of specific
How can one update an attribute value in DynamoDB table to ? one option
How do I update a value that is referencing a foreign key. Lets say
I am using code to update txtunitprice value based on other on click of
I've got a class with several properties. On every value update, a Store method
I would like a span to update when a value is entered into a
i am using a java thread to update jlabel text value in my application
I needed to update the system tray icon's text value, of my application, whenever

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.