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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:59:47+00:00 2026-06-14T11:59:47+00:00

I have the following MongoDB object: { _id: ObjectId(508ec9f413f88da4a590e5ee), beersAndStouts: { 0: { description:

  • 0

I have the following MongoDB object:

 {
   "_id": ObjectId("508ec9f413f88da4a590e5ee"),
   "beersAndStouts": {
     "0": {
       "description": "Guinness",
       "price": "4.20"
    },
     "1": {
       "description": "Heineken",
       "price": "4.80"
    },
     "2": {
       "description": "Carlsberg",
       "price": "4.80"
    }
  },
   "snacks": {
     "0": {
       "description": "King cheese and onion",
       "price": "2.20"
    },
     "1": {
       "description": "Tayto cheese and onion",
       "price": "1.80"
    }
  },
   "specialOffers": {
     "0": {
       "description": "Two for one vodka\/red bull",
       "price": "8"
    }
  },
   "uname": "Eamorr",
   "wines": {
     "0": {
       "description": "Cabernet Sauvignon",
       "price": "5.00"
    },
     "1": {
       "description": "Chardonnay",
       "price": "5.00"
    }
  }
}   

and I have the following PHP variables:

$category=$_POST['category'];   //"beersAndStouts"
$description=$_POST['columnName'];   //"Description"
$value=$_POST['value'];   //"4.70"
$rowId=$_POST['rowId'];   //2

I want to change the price of Carlsberg (rowId=2) from 4.80 to 4.70…

How to run such a query?

Here’s what I have sofar, but I’m really stuck now…

$priceLists=$mongo->eamorr->priceLists;
$priceLists->update(array('uname'=>$uname),array('$set'=>array($category=>array($rowId=>xxx))));

Update: here is the code I used:

$mongo=new Mongo();
$priceLists=$mongo->eamorr->priceLists;

$priceList=$priceLists->findOne(array('uname'=>$uname));

$newCategory=$priceList[$category];
$newCategory[$rowId][$description]=$value;

$priceLists->update(array('uname'=>$uname),array('$set'=>array($category=>$newCategory)));

It’s not pretty, but it works and I don’t have to change my structure.

  • 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-14T11:59:48+00:00Added an answer on June 14, 2026 at 11:59 am

    Why do you need it to be an object of objects?
    You can substitute it with array of objects and in such a case it will work just fine
    Here is the idea:

    {
      "_id" : ObjectId("508ec9f413f88da4a590e7ee"),
      "beersAndStouts" : [{
          "description" : "Guinness",
          "price" : "4.20"
        }, {
          "description" : "Heineken",
          "price" : 6666
        }, {
          "description" : "Carlsberg",
          "price" : "4.80"
        }],
      "snacks" : [{
          "description" : "King cheese and onion",
          "price" : "2.20"
        }, {
          "description" : "Tayto cheese and onion",
          "price" : "1.80"
        }],
      "specialOffers" : [{
          "description" : "Two for one vodka/red bull",
          "price" : "8"
        }],
      "uname" : "Eamorr",
      "wines" : [{
          "description" : "Cabernet Sauvignon",
          "price" : "5.00"
        }, {
          "description" : "Chardonnay",
          "price" : "5.00"
        }]
    }
    

    And the query in such a case is simple:

    db.test.update({
       "_id" : ObjectId("508ec9f413f88da4a590e7ee"),
       "beersAndStouts.description" : "Carlsberg"
    },{
      '$set' : {
      'beersAndStouts.$.price' :  4.70
    }
    });
    

    keep in mind that I changed ObjectId!

    Put this in your mongo shell and it works.

    I hope adjusting it to php is obvious.

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

Sidebar

Related Questions

I have the following mongodb object: { _id: ObjectId(4d0b9c7a8b012fe287547157), messages: { 0: { toUname:
Greetings, I have the following mongodb object: { _id: ObjectId(4d0b9c7a8b012fe287547157), messages: { 0: {
I have the following mongodb structure { _id: ObjectId(507c80a143188f9610000003), date: ISODate(2012-10-15T21: 31: 13.0Z), uid:
I have the following MongoDB object stored in my Mongo database: array ( '_id'
I have the following data structure: Object _id: ca06ef84-5934-498e-9887-8a3241e713f2 answers: Object owner: 273b7291-df2b-494c-bd9b-64e71283447e Topic:
I have the following Data structure where Question.findOne() returns: Object _id: 8132c2f0-baee-434f-a739-c4d30718daf2 owner: 273b7291-df2b-494c-bd9b-64e71283447e
I have a MongoDB collection with documents in the following format: { _id :
I have objects like the following: { _id : ObjectId(4f7f0d64e4b0db1e18790f10), location : [ 0.674081,
i have following kind of records in mongodb { user_id : 79102, country_code :
I have the following code to connect to MongoDB: try { $m = new

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.