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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:48:43+00:00 2026-06-03T03:48:43+00:00

In MongoDB-PHP I am using the following example code to push a new entry

  • 0

In MongoDB-PHP I am using the following example code to push a new entry to the end of an array inside a collection…

$data = array(
        "domain"=>"superduperyoyo.com",
        "number"=>123,
        "week"=>5,
        "year"=>2012
);
$db->domains->save(
        array( 'someid' => $someid),
        array( '$push' => array( 'data' => $data ))
);

This returns keys like 0,1,2,3….
ie.

[someid] => somesupercoolid123
[data] => Array
    (
        [0] => Array
            (
                [domain] => superduperyoyo.com
                [number] => 123
                [week] => 5
                [year] => 2012
            )
        [1] => Array(...)
        [2] => Array(...)
    )

What I want to do is store YearWeekNumber as the key like this…

 [someid] => somesupercoolid123 
   [data] => Array
        (
            [201205123] => Array
                (
                    [domain] => superduperyoyo.com
                    [number] => 123
                    [week] => 5
                    [year] => 2012
                )
            [201206123] => Array(...)
            [201207123] => Array(...)
        )

How do you save/update the key along with the new entry? I am assuming you can’t use $push. That you just use .save or .update but how do you pass the key?

  • 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-03T03:48:44+00:00Added an answer on June 3, 2026 at 3:48 am

    You’d do this by using $set:

    $data = array(
            "domain"=>"superduperyoyo.com",
            "number"=>123,
            "week"=>5,
            "year"=>2012
    );
    $db->domains->update(
            array( 'someid' => $someid),
            array( '$set' => array( 'data' => array( 201205123 => $data )))
    );
    

    I would however not recommend doing this. It’s better to set another key with this “201205123” value as otherwise you wouldn’t be able to do range queries on this value or set an index.

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

Sidebar

Related Questions

how to insert inner array in mongodb using php I run this command in
having this weird issue while inserting to MongoDB using PHP. My insertion code is
I successfully stored image files in MongoDB using PHP, when i retrieve images from
I would love to sort an embedded MongoDB object using PHP Lithium. I have
I'm using MongoDB and PHP and trying to do a $in based on a
I'm fairly new to PHP and MongoDB so it would be really helpful if
Following MongoDB documentation example seems to be wrong. Any suggestion? a== 1 and a
I am creating an API using PHP and MongoDB. In this system I got
created a collection in MongoDB consisting of 11446615 documents. Each document has the following
I try to delete document in mongodb using php, I it fails and I

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.