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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:03:24+00:00 2026-06-04T08:03:24+00:00

Background: Using CodeIgniter with this MongoDB library . This is my first go-round with

  • 0

Background: Using CodeIgniter with this MongoDB library.

This is my first go-round with mongodb and I’m enjoying it thus far. It’s taken a while for me to separate myself from the sql way of thinking, but it is a perfect fit for my current project.

I’m trying to push an array into a document using…

$this->mongo_db->where(array('_id'=>$estimate_id))->push("measurements", $newData)->update('estimates');

If I encode $newData using json_encode($newData) I get {"levels":[{"level_qty":12,"level_uom":"ft"}]}

The problem is, when my function creates the measurements line in the mongodb document, it automatically starts an array with my insertion at [0]. Like this…

"measurements" : [ { "levels" : [ { "level_qty" : 12, "level_uom" : "ft" } ] }]

…leaving me with…

-measurements
--0
----levels
-----0
------level_qty => 2,
------level_uom => ft

What I really want is…

-measurements
--levels
---0
----level_qty => 2,
----level_uom => ft

I’m certain I’m missing something fairly elementary (i.e. php related & not mongodb related), but I’m an admitted amateur who has waded too deep.

  • 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-04T08:03:25+00:00Added an answer on June 4, 2026 at 8:03 am

    $push is used to append a value to an array. In your example, measurements is an array and Mongo is appending $newData as its first element. This explains the 0 index between measurements and levels. In your desired result, measurements is an object equivalent to $newData (i.e. it has a levels property, which in turn has an array of objects within).

    Either of the following examples should accomplish what you want:

    // if $newData is {"levels": [{"level_qty":12,"level_uom":"ft"}]}
    ->set("measurements", $newData)
    
    // if $newData is [{"level_qty":12,"level_uom":"ft"}]
    ->set("measurements.levels", $newData)
    
    // if $newData is {"level_qty":12,"level_uom":"ft"}
    ->push("measurements.levels", $newData)
    

    Note: $push is going to be more flexible if you want to append data with future updates, whereas $set will naturally overwrite the given field.

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

Sidebar

Related Questions

I'm trying to set an image as the background using this code: root.setStyle(-fx-background-image: url('splash.jpg');
i'm using the dompdf library with Codeigniter (php 5 ++) , everythings works great,
I'm using Codeigniter 2. I'm trying to display some images using the background url
How do I separate the body/content area of my site from the background using
See this MSDN post for some background: Using metadata to call a target multiple
I'd like to know how to download an image asynchronously. Using: Codeigniter, JQuery. Background:
Background Using boost and other similar libraries is the easiest way to find compiler
Background: Using jQuery 1.7 client side PHP server side Using json responses with json_encode
Background Using JavaScript, I need to sort a large JSON object based on a
I'm trying to create a new spinner background using a 9 patch picture. I've

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.